Linked List Midpoint

LeetCode

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

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

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
slow
fast
1
0
2
1
3
2
4
3
5
4
Step 1 / 3
Step 1:
Initialize slow and fast at head.
Pointers: fast=0, slow=0
Focus: select @ [0]