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)
Finding the Entrance of a Linked List Cycle (1 → 2 → 3 → 4 → 5 → 3) Floyd's Tortoise & Hare Algorithm: Phase 1 (Meeting Point) & Phase 2 (Cycle Entrance) Head Node 1 val: 1 Node 2 val: 2 ENTRANCE Node 3 Loop Node Node 4 Loop Node Node 5 5.next points to 3 🐢 & 🐇 Meet Here 1. The Infinite Trap • Standard traversal freezes & loops infinitely through 3→4→5. • Knowing a cycle exists is not enough; you must find exact gate (Node 3) to repair pointers. 2. Two-Pointer Magic O(1) • Phase 1: Fast & slow pointers meet inside loop (arbitrary spot). • Phase 2: Reset one pointer to head; step both by 1. They collide exactly at Node 3! 3. Python Implementation def detectCycle(head): slow = fast = head while fast and fast.next: slow, fast = slow.next, fast.next.next if slow == fast: # match! return findEntrance(head, slow) 💡 Why Does Phase 2 Work Geometrically? Distance from Head to Entrance ($L$) equals the distance from meeting point to Entrance ($K$) along the loop. Walking one pointer from Head and another from Meeting Point at equal speed guarantees they lock onto Node 3 simultaneously.
Entrance of 1→2→3→4→5→3… overview diagram
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.
Why We Need to Find the Start of a Linked List Cycle Concrete Example: 1 → 2 → 3 → 4 → 5, with Node 5 looping back to Node 3 Node 1 val: 10 Node 2 val: 20 Node 3 CYCLE START Target Fix Node 4 val: 40 Node 5 Corrupted next pointer loops back! Structural Threshold ! The Infinite Trap & Memory Leak • Standard algorithms (printers, length checkers) enter an endless silent loop: 3 → 4 → 5 → 3... • Causes CPU exhaustion or OOM kills in prod. Result: Service crash without pinpointing root cause. ✓ Why Locate the exact Entrance? • Repairs Pointer: Safely sever cycle (set node 5 .next = null). • O(1) Space Math: Avoids heavy HashSets for memory tracking. • Floyd's Tortoise & Hare math zeroes in on Node 3 precisely. Result: Clean memory diagnostics & zero-alloc repairs.
Why We Need to Find the Start of a Linked List Cycle diagram
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.
Mental Model: Pointers on a Circular Track (Linked List Cycle) Straight access path (1→2) feeding into a circular loop (3→4→5→3) Head Node 1 Access Node 2 ENTRANCE Node 3 Node 4 COLLISION Node 5 5.next points back to 3 (Loop!) 🐢 Tortoise (1x) 🐇 Hare (2x) 1. The Bounded Trap • Access path (1→2) leads straight into loop entrance (Node 3). • Once inside the ring (3→4→5), pointers are trapped forever. 2. Arbitrary First Meeting • Hare (2x speed) laps Tortoise (1x speed) at Node 5. • Node 5 is NOT the cycle entrance—it's just a speed collision site!
Mental Model: Pointers on a Circular Track diagram
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.
python
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
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.
python
def detectCycle(head: ListNode) -> ListNode:
    # TODO: Implement Phase 1 (find meeting point) and Phase 2 (find entrance)
    pass
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, slow and fast, at the head (node 1). slow advances by 1 step, while fast advances by 2 steps on each iteration.
- Start: slow = 1, fast = 1
- Iteration 1: slow = 2, fast = 3
- Iteration 2: slow = 3, fast = 5
- Iteration 3: slow = 4, fast = 4
At 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 head to the cycle entrance equals the distance from the meeting point (4) to the cycle entrance, walking step-by-step.
- Reset pointer 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

Both p1 and p2 meet at node 3. The algorithm successfully returns node 3 as the start of the linked list cycle.
python
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def detectCycle(head: ListNode) -> ListNode:
    slow = fast = head
    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next
        if slow == fast:
            # Trace phase: complete the realignment logic here
            p1 = head
            p2 = slow
            while p1 != p2:
                p1 = p1.next
                p2 = p2.next
            return p1
    return None
Phase 4: Tracing Floyd's Algorithm & Cycle Entrance Alignment List: 1 → 2 → 3 → 4 → 5 (Node 5 links back to Node 3. Entrance = Node 3) Phase A: Finding Meeting Point (Iteration 3 → Both at Node 4) 1 Head 2 3 Entrance 4 Meeting (slow/fast) 5 5 links back to 3 Iteration 3 Collision: • slow = Node 4 (1 step) • fast = Node 4 (2 steps) Both collide at Node 4! Phase B: Finding Cycle Entrance (Distance Head-to-Entrance = Meeting-to-Entrance) 1 p1 (Head) 2 3 p1 & p2 Meet! 4 p2 (Meeting) 5 Loop connection Realignment & Step Trace: 1. Reset: • p1 = Head (Node 1) • p2 = Meeting (Node 4) 2. Advance 1 step each: • Step 1: p1 → 2, p2 → 5 • Step 2: p1 → 3, p2 → 3 Result: Return Node 3 (Entrance)
Tracing the Start of the 1→2→3→4→5→3 Cycle Step by Step diagram
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.
python
class ListNode:
    def __init__(self, val=0):
        self.val = val
        self.next = None

# Given the list 10 -> 20 -> 30 -> 40 -> back to 10
# Write or trace what detectCycleEntrance(head) returns.
def detectCycleEntrance(head):
    # TODO: Implement Floyd's cycle-finding and realignment algorithm
    pass
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.

Keep learning