Skip to content
DSA Visualizerpreview

Detect a cycle (tortoise and hare)

Two pointers, one twice as fast: if the list loops, the fast one catches the slow one.

O(n) time · O(1) space

List3, 1, 4, 1, 5, 9, 2loops to 2

Both pointers start at the head.

List 3 → 1 → 4 → 1 → 5 → 9 → 2, and the last node points back to 4.
  • compare
  • swap or write
  • settled

1 / 6

Speed

Space play · ← → step · Home End jump · drag the strip to scrub