Leetcode 分享筆記

876. Middle of the Linked List

題目

leetcode
b

python


class Solution(object):
    def middleNode(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        slow = head # ListNode slow
        fast = head # ListNode fast
        while fast:
            if fast.next ==None or fast.next.next == None:
                if fast.next != None:
                    return slow.next
                else:
                    return slow
            slow = slow.next
            fast = fast.next.next