Problem Statement
Middle of the Linked List
You are given the head of a singly linked list. A linked list is a chain of nodes, where each node holds a value and a pointer to the next node. "Singly" means each node only knows about the one in front of it, not the one behind. The "head" is the first node in the chain. Your job is to return the middle node. If the list has two middle nodes (an even number of nodes), return the second of the two.
Signals to notice
Brute force first
Count length, then traverse to length/2 — two passes. That instinct is useful because it follows the prompt literally, but it usually keeps revisiting work the problem is begging you to organize.
The key insight
Slow moves 1, fast moves 2 — when fast reaches end, slow is at middle — single pass. The goal is not to be clever for its own sake, but to remember the one relationship that keeps the solution grounded as you move forward.
Trace it on head = [1,2,3,4,5]
init: slow=node(1), fast=node(1) fast & fast.next (1,2) -> slow=node(2), fast=node(3) fast & fast.next (3,4) -> slow=node(3), fast=node(5) fast=node(5), fast.next=null -> loop stops return slow=node(3) -> [3,4,5]
What must stay true
Fast moves twice as fast, so when it finishes, slow has covered exactly half. If that remains true after every update, the rest of the reasoning has a stable place to stand.
Shape of the loop
slow = head
fast = head
while fast and fast.next:
slow = slow.next # advances 1
fast = fast.next.next # advances 2
return slow # middle when fast hits endPseudocode only — the full worked solution lives in the Solution tab.
Easy way to go wrong
Off-by-one for even-length lists — decide if you want the first or second middle node. Most mistakes here are not about syntax; they come from losing track of what your state, pointer, or structure is supposed to mean.