Fast & Slow Pointers
TimeO(n)SpaceO(1)
Two pointers move at different speeds. If the list has a cycle, the fast pointer laps the slow one and they meet; if it reaches the end, there's no cycle.
Key idea. Floyd's tortoise & hare: a fast pointer laps a slow one inside any loop. Detects cycles without a visited set.
1
Loading…
—