Skip to main content

04 Fast and Slow Pointers - Cycle Detection

Requirement - a Linked list or similar structure which can form cycles
Core Problem - Find if there are cycles in our LinkedList / Graph
Core Logic : Fast pointer moves 2 steps at a time thus is able loop the cycle if there is any, and stab the slow pointer on the back

534

Steps:​

    1. Initialize two pointers, one moving one step at a time (slow) and the other moving two steps at a time (fast).
  1. If there is a cycle, the fast pointer will eventually meet the slow pointer.
  2. If the fast pointer reaches the end of the list, there is no cycle.

Sample Problem:​

Detect if a linked list has a cycle.

LeetCode Problems:​

  1. Linked List Cycle (LeetCode #141)
  2. Happy Number (LeetCode #202)
  3. Find the Duplicate Number (LeetCode #287)