Linked List Cycle
A fast pointer laps a slow one if and only if there's a loop.
Approach
Advance one pointer by one and another by two each step. If they ever meet, there's a cycle; if the fast pointer reaches the end, there isn't. Floyd's trick detects loops in O(1) space, unlike a visited-set approach.
Time complexity
O(n)
Space complexity
O(1)
Common mistake
Checking pointer identity before advancing, or not guarding fast.next before taking two steps.
See it run, step by step
Generate an interactive lesson for Linked List Cycle — trace every variable and watch the algorithm execute until it clicks.
Start a lesson on Linked List Cycle