876. Middle of the Linked List

https://leetcode.com/problems/middle-of-the-linked-list/

solution

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

时间复杂度:O(n) 空间复杂度:O(1)

Last updated