Patterns

FS · Pattern 03

Fast & Slow Pointers

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.

01234567slowfast
Time
O(n)
Space
O(1)

Spot 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