advanced11 min read·Updated September 22, 2026
Start of a Linked List Cycle Explained: Tracing 1→2→3→4→5→3
Master cycle entrance detection using Floyd's algorithm. Walk through the 1→2→3→4→5→3 example, mental models, and edge cases on Learnisim.
By Learnisim AI·Published September 22, 2026
LEARNING ARCSetup → Model in the example → Full trace → Twist → Edge cases → Apply
PREREQUISITES
- Basic linked list traversal
- Pointers and memory references
- Basic cycle detection (Tortoise and Hare)
Why
Why We Need to Find the Start of a Linked List Cycle
Phase 1: The Infinite Trap of Corrupted Pointers
Imagine you are debugging a memory leak in a critical backend cache system built from raw nodes. You encounter a corrupted linked list represented by our locked example: the nodes link sequentially as 1 → 2 → 3 → 4 → 5, but node 5 has its
next pointer erroneously redirected backward to node 3. If you pass this structure into a standard traversal algorithm like a tree printer or a length counter, your program does not crash—it simply freezes, looping silently through 3 → 4 → 5 → 3 → 4 → 5 until the operating system kills it for out-of-memory or CPU exhaustion.Detecting that a cycle exists using two pointers (the tortoise and hare algorithm) is a classic trick, but it only tells you that a loop is present. It leaves you stranded somewhere swirling inside the ring at node 4 or 5. In production, simply knowing the structure is broken is insufficient; you need to repair the pointer or isolate the corruption. To fix it, you must locate the exact structural threshold where the linear path collapses into an endless loop: the entrance node, which for our example is node 3.
Without a precise space method to pinpoint node 3, developers are forced to allocate hash sets to track every visited memory address, introducing heavy memory overhead. We need a mathematical approach that finds the exact point of entry without burning extra memory.
Model
Mental Model: Pointers on a Circular Track
Phase 2: The Racetrack Model
Imagine the linked list
1 -> 2 -> 3 -> 4 -> 5 where 5.next points back to 3. This is not a straight highway; it is a straight access path followed by a circular racetrack. In our locked working example, the access path spans nodes 1 and 2 before the track loop begins at node 3.When a tortoise pointer and a hare pointer enter this layout, they do not just spin aimlessly. The hare races ahead at double speed while the tortoise creeps forward one node at a time. Because the loop traps them in a bounded space, the faster hare is guaranteed to lap the tortoise, forcing a collision at some specific node inside the loop—far past the entrance.
The core mystery of cycle detection is that this first meeting point is an arbitrary collision site dictated by speed ratios, not the true structural bottleneck of the list. To locate node 3—the exact gate where the cycle begins—we need a geometric perspective that connects the distance from the head to the entrance with the remaining distance inside the loop.
Syntax
Syntax and APIs for Finding the Linked List Cycle Entrance
Phase 3: Writing the Cycle Detection Code
Translating our racetrack model into a concrete implementation requires standard pointer structures. For our locked working example where nodes hold values 1 through 5 and node 5 points back to node 3, we define a standard singly-linked list node class.
The function signature accepts the
head of this list and must return the exact ListNode where the loop begins (Node 3), or None if no loop exists. A common syntax mistake is forgetting that fast and slow must traverse at different speeds during phase one, or prematurely modifying the head pointer before the second phase begins.Worked example
Tracing the Start of the 1→2→3→4→5→3 Cycle Step by Step
Phase 4: Worked Example
To see how Floyd's cycle detection algorithm pinpoints the exact entry node, let us trace our locked example: a linked list containing nodes , where node 5 links back to node 3. The entrance to the cycle is node 3.
Given
•A linked list head pointing to node 1.
- The cycle loop consists of nodes .
•Expected result of our procedure: return the reference to node 3.
Steps
Phase A: Detecting the Meeting Point
We initialize two pointers,
We initialize two pointers,
slow and fast, at the head (node 1). slow advances by 1 step, while fast advances by 2 steps on each iteration.- Start:
- Iteration 1:
- Iteration 2:
- Iteration 3:
slow = 1, fast = 1- Iteration 1:
slow = 2, fast = 3- Iteration 2:
slow = 3, fast = 5- Iteration 3:
slow = 4, fast = 4At Iteration 3, both
slow and fast point to node 4. They have collided inside the cycle, confirming a cycle exists.Phase B: Finding the Cycle Entrance
Per mathematical proof, the distance from the
Per mathematical proof, the distance from the
head to the cycle entrance equals the distance from the meeting point (4) to the cycle entrance, walking step-by-step.- Reset pointer
- Keep pointer
- Step 1:
- Step 2:
p1 to the head (node 1).- Keep pointer
p2 at the meeting point (node 4).- Step 1:
p1 moves to 2; p2 moves through the loop to 5.- Step 2:
p1 moves to 3 (the cycle entrance); p2 moves through the loop to 3 (the cycle entrance).Result
Bothp1 and p2 meet at node 3. The algorithm successfully returns node 3 as the start of the linked list cycle.Practice
Practice Finding the Linked List Cycle Entrance
Phase 5: Practice
Now it is your turn to apply the cycle entrance detection algorithm to a slightly modified variation of our locked working example. Recall that for our original list , the tortoise and hare met inside the loop, and realigning one pointer to the head yielded node 3 as the entrance.
Imagine a new scenario where the linked list is structured as , and node 40.next points directly back to node 10, creating a cycle at the very beginning of the structure. Your task is to trace how the slow and fast pointers interact in this layout and determine where the realignment phase places your pointers to find the entrance.
Edge cases
Edge Cases and Subtle Traps in Cycle Entrance Detection
Even though our locked working example 1→2→3→4→5→3… resolves cleanly at node 3, real-world data structures hide structural anomalies that crash naive cycle algorithms. What happens if the cycle begins at the absolute head of the list instead of an offset? Consider a modified version of our linked list where node 1 points to 2, 2 points to 3, and 3 loops directly back to 1. Here, head is already the entrance, yet the tortoise and hare will still meet somewhere deep inside the loop before realignment starts.
Another fatal trap is an entirely acyclic list where
fast or fast.next hits null. If you pass a standard linear list 1→2→3→4→5 into your cycle-entrance detector without guarding against null pointers, your code will throw a NullPointerException during the phase-one traversal when evaluating fast.next.next. You must always return null immediately if the fast pointer reaches the end of the chain.Single-node self-loops (
1→1) introduce yet another geometric edge case. When a single node points to itself, head, slow, and fast all initially point to node 1. During the very first iteration, fast and slow may collide instantly at node 1, or skip past depending on how the loop condition is written. Ensuring your loop condition checks fast != null && fast.next != null and handles zero-length or single-node boundaries safely keeps your pointer arithmetic from sliding into infinite loops or segmentation faults.Apply
Applying Floyd's Cycle Mathematics Beyond Linked Lists
Phase 7: Transfer
Having mastered the mechanics of locating the exact node where a linked list loops back on itself, we can now step back and recognize that this is not just a pointer-chasing trick—it is a fundamental property of finite directed graphs with out-degree one. In our locked example , the math worked because every node has exactly one outgoing pointer, forcing paths to eventually merge into a periodic tail. This same geometric invariant appears across completely different domains of computer science where memory overhead must remain strictly bounded at .
Consider the classic problem of finding a duplicate number in an read-only array of integers where each integer is between 1 and (known as the pigeonhole principle array problem). By treating the array values as pointers (where ), the array instantly morphs into an implicit linked list with a cycle. Finding the duplicate number becomes identical to finding the cycle entrance of our locked example. If you pass an array like , the sequence of index jumps produces a cycle, and the tortoise-and-hare realignment algorithm isolates the exact repeating value without modifying the array or using a hash set.
Another powerful application is detecting infinite loops in state transition tables, game board paths (like Snakes and Ladders with deterministic wraps), or pseudo-random number generator period lengths. Whenever a deterministic function maps a finite domain onto itself, you are walking a functional graph. The realization that the distance from the head to the cycle entrance equals the distance from the meeting point back to the entrance is a universal structural law of deterministic iteration.
FAQ
Why is the meeting point of the tortoise and hare not the start of the cycle?
The fast pointer (hare) travels twice as fast as the slow pointer (tortoise), meaning they will always collide somewhere inside the loop, not necessarily at the exact entry node.
In the example 1→2→3→4→5→3, where do the pointers meet and what is the entrance?
Depending on pointer pacing, they typically meet at node 4 or 5, but mathematical reset mechanics guarantee that resetting one pointer to the head will cause both to land precisely on node 3 (the entrance).
What is the time and space complexity of finding the cycle entrance?
The time complexity is O(N) to traverse the list and find the loop, and the space complexity is O(1) because it uses only two pointers regardless of list size.
What happens if there is no cycle in the linked list?
If the fast pointer or its next reference reaches null, it indicates a terminating list with no cycle, meaning no cycle entrance exists.