Input: head = [1, 2, 3, 4, 5] → Output: node 3
Return the middle node. If two middle nodes, return the second middle.
TimeO(n)single pass
SpaceO(1)only two pointers
Iterations: 0Slow: —Fast: —
Slow (tortoise)
Fast (hare)
Middle
Slow
—
Fast
—
Result
pending
Ready
Press Play to watch the fast & slow pointer technique find the middle, or Step to advance one operation. Slow moves 1 step, fast moves 2 steps. When fast reaches the end, slow is at the middle.
TimeO(n)single pass
SpaceO(n)call stack depth n/2
✎ Whiteboard
3
⌨ Type It — Two Pointer
Practice until you don't need to look. The green highlights are the nuances to burn into memory.
═══ MIDDLE OF LINKED LIST — TWO POINTER ═══
PATTERN ▸ Fast & slow pointer O(n) · O(1)
① SETUP — BOTH AT HEAD
slow = head
fast = head
② LOOP — FAST HAS ROOM
while fast and fast.next:
③ MOVE — SLOW 1, FAST 2
slow = slow.next
fast = fast.next.next
④ RETURN MIDDLE
return slow
Vars: slow, fast
▼ your implementation ▼
⌨ Type It — Recursive
Practice until you don't need to look. The green highlights are the nuances to burn into memory.