Fast & Slow Pointers

Move two pointers at different speeds through a sequence to detect cycles or locate structural midpoints in O(1) space (Floyd's algorithm).

Move two pointers at different speeds through a sequence to detect cycles or locate structural midpoints in O(1) space (Floyd's algorithm).

If a cycle exists, the fast pointer (2× speed) closes the gap by 1 per step and must eventually lap the slow pointer.

Floyd's cycle theorem: in a cyclic structure the fast pointer gains one position per step on the slow pointer, guaranteeing a collision within one full cycle length.

No visited set is needed — the pointer collision is the cycle signal, keeping extra space O(1).