Pattern GuideFast & Slow Pointers

Fast & Slow Pointers

"Two runners on the same track. Different speeds, certain collision."

8 min read Fundamental Space: O(1)
01
CORE INTUITION

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.

02
PATTERN

Cycle Detection

Slow +1, Fast +2 โ€” they meet if a cycle exists

SLOWFASTABCDEF

Both pointers start at head (A).

1 / 5

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.

03
PATTERN

Find the Middle

Slow +1, Fast +2 โ€” Fast falling off the end means Slow is at the middle

Both pointers start at head (10).
SLOW
10
FAST
20
30
40
50
60
1 / 4

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.

04
BLUEPRINTS

Golden Templates

1. Cycle Detection

hasCycle.js
1
function hasCycle(head) {
2
let slow = head, fast = head;
3
while (fast && fast.next) {
4
slow = slow.next;
5
fast = fast.next.next;
6
if (slow === fast) return true; // they collided โ€” cycle!
7
}
8
return false; // fast fell off the end โ€” no cycle
9
}

2. Find the Middle

middleNode.js
1
function middleNode(head) {
2
let slow = head, fast = head;
3
while (fast && fast.next) {
4
slow = slow.next;
5
fast = fast.next.next;
6
}
7
return slow; // fast hit the end โ€” slow is the middle
8
}

3. Cycle Entry Point (Phase 2)

detectCycleStart.js
1
function detectCycleStart(head) {
2
let slow = head, fast = head;
3
while (fast && fast.next) {
4
slow = slow.next;
5
fast = fast.next.next;
6
if (slow === fast) {
7
// Phase 2: reset one pointer to head, move both at speed 1
8
let ptr = head;
9
while (ptr !== slow) {
10
ptr = ptr.next;
11
slow = slow.next;
12
}
13
return ptr; // the cycle's entrance
14
}
15
}
16
return null; // no cycle
17
}
05
RECOGNITION

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

06
PITFALLS

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."

07
PRACTICE

Ready to Practice?

Linked List CycleEasy
Linked List Cycle IIMedium
Middle of the Linked ListEasy

"If you run twice as fast as me, you'll eventually lap me on a loop."