LeetCode 876 Middle of the Linked List (Python)

Posted by 小明MaxMing on April 26, 2020

题目

Given a non-empty, singly linked list with head node head, return a middle node of linked list.

If there are two middle nodes, return the second middle node.

解题思路

代码

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def middleNode(self, head: ListNode) -> ListNode:
        fast = slow = head
        while fast and fast.next:
            fast = fast.next.next
            slow = slow.next
        return slow

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅