MentorNode
Start free

Pattern 03 of 08 · fast-slow-pointers

Fast & Slow Pointers

Traverse linear structures (linked lists, sequence graphs) at two distinct speeds to detect cycles and identify midpoint boundaries.

Builds on
Two Pointers
Unlocks
The end of this branch.

See the invariant

Step through it
Pattern InvariantFast & Slow Pointers

Floyd's Cycle Finding Algorithm (Tortoise & Hare)

Step 1 of 5
Slow/Fast
A
Node 0
B
Node 1
C
Node 2 (Cycle Start)
D
Node 3
E
Node 4
F
Node 5
links back to [Node 2]

Initial state: Both Slow (the tortoise) and Fast (the hare) start at head Node 0 (A).

Speed:

Practice it

3 problems
  1. 1Linked List Cycle Detectionlinked-list-cycleeasy
  2. 2Middle of the Linked Listmiddle-of-the-linked-listeasy
  3. 3Find the Duplicate Numberfind-the-duplicate-numbermedium