AlgoPlus//structures / fast-slow-pointers
Read the theory

Fast & Slow Pointers · Cycle Detection

Floyd's tortoise and hare — if there's a loop, the fast pointer laps the slow one.

Nodes
8
Cycle
yes
Status
moving
Legend
Slow (+1)
Fast (+2)
Meeting point
AI Tutor Workspace
In a nutshell
Also called Floyd's tortoise and hare, this detects a loop in a linked structure using two pointers from the same start: a slow one stepping 1 node at a time and a fast one stepping 2. If the path eventually loops, the fast pointer circles around and catches the slow one from behind, so they meet. If there's no loop, the fast pointer just runs off the end. It needs only those two pointers — constant extra memory.
Ready
Press play to begin the cinematic walkthrough.
Two pointers move at different speeds through a linked structure. If there's a cycle, the fast one laps the slow one and they meet.
Key terms
Go deeper in the lesson
Read the full theory, intuition & complexity for Fast & Slow Pointers · Cycle Detection.