algorythms
All Patterns
Pattern 3

Fast & Slow Pointers

01234567slowfast

Floyd's cycle detection: one pointer moves twice as fast. They'll meet if there's a cycle, or fast will hit null if there isn't.

Time

O(n)

Space

O(1)

Recognize it when

  • Detect cycle in linked list or array
  • Find middle of linked list in one pass
  • Detect if number is happy (infinite loop detection)
  • Find start of cycle
Progress0/4
0 solved0 attempted