Fast reaches the end; slow is at the middle
Slow walks one node, fast walks two. When fast runs out, slow sits on the second middle for even length.
Fast covers twice the ground, so it finishes in n/2 slow steps — exactly the midpoint.
Algorithm
- Both pointers start at the head.
- While fast can take two steps, move slow once and fast twice.
- Return the suffix starting at slow.
- Time
- O(n)
- Space
- O(1)
class Solution: def middleNode(self, head: list[int]) -> list[int]: slow = fast = 0 n = len(head) while fast + 1 < n: slow += 1 fast += 2 return head[slow:]