The Tortoise and the Hare
Two Runners, One Track
The Fast & Slow pointer technique (Floyd's Cycle-Finding Algorithm) uses two pointers that traverse at different speeds. By moving the Fast pointer at twice the speed of the Slow pointer, if there is a cycle, Fast must eventually lap Slow from behind โ like two runners on a circular track.
This gives us two powerful capabilities with just O(1) extra space: detecting cycles and finding the middle of a linked list.
Cycle Detection
Slow +1, Fast +2 โ they meet if a cycle exists
Both pointers start at head (A).
How Cycle Detection Works
Move Slow by 1 step and Fast by 2 steps. If Fast reaches null, no cycle. If Fast and Slow meet at the same node, a cycle exists.
Why It Works
Once both pointers are inside the cycle, the gap between them shrinks by 1 every step (Fast gains 2, the cycle "returns" 1 relative position). A shrinking gap on a finite loop must eventually reach 0 โ they collide.
Finding the Cycle Start
After detection, reset one pointer to head. Move both at speed 1. Their meeting point is the cycle's entrance (see the golden template below).
Also works for: Finding duplicate numbers in an array (treat values as pointers) โ Happy Number uses the same collision idea on a sequence of digit-square sums.
Find the Middle
Slow +1, Fast +2 โ Fast falling off the end means Slow is at the middle
How Finding the Middle Works
Move both pointers from head. When Fast reaches the end (or runs past it), Slow is exactly at the midpoint. For even-length lists like the 6-node example above, Slow lands on the second of the two middle elements (right bias) with the while (fast && fast.next) loop shown here.
Essential for: Merge Sort on linked lists (split into two halves), binary search on linked lists.
Golden Templates
1. Cycle Detection
2. Find the Middle
3. Cycle Entry Point (Phase 2)
How to Spot This Pattern
๐ "Determine if a linked list has a cycle."
๐ "Find the middle node" in one pass, O(1) space.
๐ A sequence defined by "apply this function to get the next value" that might loop forever.
๐ "Find the duplicate number" without modifying the array or using extra space.
Named problems: Linked List Cycle I & II ยท Middle of the Linked List ยท Happy Number ยท Find the Duplicate Number
Common Mistakes
Checking fast.next.next Unsafely
You must check fast && fast.next BEFORE reading fast.next.next โ checking only fast lets fast.next be null, and null.next crashes.
Starting Fast at head.next
Starting both pointers at head vs. starting Fast one step ahead changes which of the two middle elements you land on for even-length lists. Pick one convention and be consistent โ problems sometimes specify which middle they want.
Advancing Slow Before the Null-Guard
Moving Slow (or Fast) before re-checking the loop condition can advance a pointer past a null node, throwing instead of cleanly returning "no cycle" / "list too short."
Ready to Practice?
"If you run twice as fast as me, you'll eventually lap me on a loop."