Skip to content
Optimal Lab

DSA · interactive

Fast and slow pointers

Does this linked list loop back on itself? Send two pointers along it, one twice as fast. If there's a loop, the fast one laps the slow one and they meet. No extra memory needed.

Phase 1: is there a cycle?

  1. 0slowfast→
  2. 1→
  3. 2→
  4. 3→
  5. 4→
  6. 5→
  7. 6→
  8. 7
  9. → back to 3

Both start at the head. Slow moves 1 step, fast moves 2.

The list

move 0 of 9
  1. 1

    Slow takes one step, fast takes two, every round.

    slow = slow.next; fast = fast.next.next

  2. 2

  3. 3

  4. 4

In production

Tools and technologies

Floyd's trick appears wherever memory is tight.

Not tied to one language — use these names when you search docs or design a system.