Skip to content

Floyd's cycle detection: slow, fast, then reset

IntermediateAsked sometimesCoding roundCodingLinked Lists
Asked atAmazonMicrosoftAdobe
#linked-list#two-pointers#cycle-detection#floyd

Company tags are compiled from public interview reports and are signals, not guarantees.

What interviewers are testing

Floyd's tortoise-and-hare is a favorite because the code is five lines but the proof is not. Interviewers watch whether you can argue that the pointers must meet, and whether you realize the meeting point is not necessarily the cycle entrance. The reset trick and the distance argument behind it separate candidates who memorized the algorithm from those who can derive it. It also tests space-awareness: a visited set works, but interviewers want to hear why O(1) space matters here.

Mental model

A slow pointer moves one node per step and a fast pointer moves two. If the list ends, there is no cycle. If there is a cycle, both pointers eventually enter it, and once they are inside, the forward distance from fast to slow shrinks by exactly one node per step, so it must reach zero. That meeting proves a cycle; resetting slow to the head and then advancing both one step at a time makes them meet again, this time at the cycle entrance.

Step-by-step solution

  1. Step 1: The loop that never ends

    Traversal code usually assumes the list ends: follow next until null. A cycle breaks that assumption, and the traversal spins forever without any error - the classic production hang in parsers, walkers, and garbage collectors. Before doing expensive work like reversing a list or serializing it, you need to know whether the structure terminates. The brute-force detector keeps a set of every visited node and flags the first repeated reference; it works, but it stores one entry per node, so it costs O(n) extra memory and cannot run where memory is tight. The animation shows a walk over 3 -> 2 -> 0 -> -4 -> 5 -> back to 0: after five steps the walk revisits node 0 and would keep repeating the same three nodes forever. That picture is the motivation for a detector that uses constant memory.

  2. Step 2: Slow and fast enter

    The algorithm is two pointers moving over the same list at different speeds. Slow advances one node per step; fast advances two. If the list ends, fast or fast.next becomes null and the answer is no cycle. If a cycle exists, both pointers are eventually trapped in it, because every next pointer inside the cycle stays inside. The animation follows the crossing point: head 3, then 2, then the entrance at 0. From then on fast keeps lapping the same three nodes. The key quantity is the forward distance from fast to slow inside the cycle. While moving, fast gains one position on slow per step, so that distance shrinks by exactly one each step - and once both pointers are on the cycle, a distance that starts below the cycle length must eventually reach zero. Fast can never jump over slow, because the gain is one node, not two.

  3. Step 3: Meeting proves a cycle

    Trace the race from the top with the full sequence. Both pointers leave the head 3 together. After one step slow is at 2 and fast is at 0, already inside the loop. After two steps slow is at 0 and fast is at 5 - both are on the cycle, with a forward gap of one node. After three steps both land on -4, and that shared node is the meeting. The animation highlights the wrap: fast has no null to stop at, because node 5 points back to node 0. If fast reached null instead, the list would be acyclic and no meeting could occur, which is why the loop must also test fast.next for even-length lists. The meeting proves a cycle exists, but the meeting node depends on the head-to-entrance distance and usually is not the entrance itself.

  4. Step 4: Reset to find the entrance

    A meeting proves a cycle exists but usually not where it begins - the meeting point depends on the head-to-entrance distance and the cycle length. The entrance is found with one elegant move: leave fast at the meeting point, reset slow to the head, and then advance both pointers one node at a time. They meet again, and this time the meeting node is the cycle entrance. The distance argument is short: when they first meet at index a + b, slow has walked a + b steps, fast has walked a + b + kL for some whole number k, and fast walked twice as far, so a + b = kL. That makes the distance from the head to the entrance equal to the distance from the meeting point to the entrance, modulo the cycle length - two equal-speed pointers leaving those positions must arrive together. The animation shows exactly that: reset to 3, advance to 2, advance to 0, entrance found.

  5. Step 5: O(1) space beats a set

    The alternative detector is a visited set: walk the list, insert each node, and stop when a node repeats. It is simpler to reason about and reports the first repeated node immediately, but it allocates one entry per node, so it uses O(n) memory and cannot run where allocation is expensive or forbidden. Floyd uses two references and no allocation at all. Its cost is still O(n) time: the pointers traverse the list at most a few times before meeting, and the reset phase adds at most one more pass, so the constants are small even though the asymptotics match the set approach. The animation contrasts the two: the set version accumulates five entries for this list, while the pointer version carries only slow and fast. When memory is cheap the set is easier to defend; when it is not, the two pointers are the answer.

Edge cases & traps

  • Cycle at the head: the reset phase still converges at the head, but code that assumes the entrance differs from the meeting point breaks - trace [1, 2] with 2 pointing back to 1 to verify.
  • A single node pointing to itself: fast must check fast and fast.next before the double step, or the second hop dereferences null.
  • Even-length acyclic lists: fast.next is null while fast is not, so the loop must test both conditions rather than only fast.
  • Stopping at the first meeting: the meeting node is the entrance when the head-to-entrance distance is congruent to 0 modulo the cycle length (a ≡ 0 (mod L)), not only when a = 0 — always run the reset phase before claiming the entrance.
  • Mutating nodes to mark visited (for example stealing a spare flag) corrupts the list for concurrent readers; when no allocation is allowed, Floyd is the safe alternative.

Follow-up questions