Skip to content

Linked List Midpoint

LeetCode

01 · Question

Given the head of a linked list, return the middle node. If there are two middle nodes, return the second one.

02 · Solution

Reference solution

1def middleNode(head: Optional[ListNode]) -> Optional[ListNode]:
2 slow = head
3 fast = head
4 while fast and fast.next:
5 slow = slow.next
6 fast = fast.next.next
7 return slow