Find the Entry Point of a Rho-Shaped Index Chain
Linked ListsAuto-graded · 16 test casesLast updated
Tests cycle detection on implicit linked lists encoded as index arrays, applying Floyd's tortoise-and-hare algorithm to find where a cycle begins.
Rho-Shaped Index Chain
You are given a list next_node of length n, where next_node[i] is the index of the node that node i points to. This encodes an implicit singly-linked list starting at index 0.
It is guaranteed that following the chain from index 0 will eventually enter a cycle (making the shape of the Greek letter ρ — a "tail" leading into a loop).
Your task is to return the index where the cycle begins — the first node that is visited twice when you follow the chain starting from index 0.
How to read the chain
Starting at 0, you repeatedly move to next_node[current]. For example, if next_node = [1, 2, 3, 1], the traversal is:
0 → 1 → 2 → 3 → 1 → 2 → 3 → ...
The cycle entry point is 1 because that is the first node revisited.
Constraints
2 ≤ n ≤ 20000 ≤ next_node[i] < nfor alli- Following the chain from index
0is guaranteed to reach a cycle. - The tail (pre-cycle portion) may have length
0(meaning index0itself is inside the cycle). - There is exactly one cycle in the reachable portion of the chain.
Examples
Example 1
Input: find_cycle_entry([1, 2, 3, 1])
Output: 1
Example 2
Input: find_cycle_entry([2, 0, 2])
Output: 2
Example 3
Input: find_cycle_entry([1, 2, 3, 4, 2])
Output: 2
Hints
Hint 1
Use two pointers (slow moves one step, fast moves two steps) until they meet inside the cycle.
Hint 2
After the tortoise and hare meet, reset one pointer to the start (index 0) and advance both one step at a time; they meet exactly at the cycle entry.
Hint 3
The tail length can be zero — make sure your implementation handles the case where index 0 is already part of the cycle.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem is a direct application of Floyd's Cycle Detection Algorithm (tortoise and hare) on an implicit linked list encoded in an index array.
Phase 1 — Detect a meeting point inside the cycle
- Initialize
slowatnext_node[0]andfastatnext_node[next_node[0]](both start one or two steps from node0). - Advance
slowby one step andfastby two steps each iteration. - Because the reachable subgraph contains exactly one cycle, they are guaranteed to meet at some node inside the cycle.
Phase 2 — Locate the cycle entry point
A classic Floyd invariant guarantees that the distance from 0 to the entry point equals the distance from the meeting point to the entry point (traveling forward inside the cycle). Therefore:
- Reset a
finderpointer to index0. - Advance both
finderandslowone step at a time. - The first node at which they are equal is the cycle entry point.
Edge cases
- Tail length 0 (node
0is inside the cycle): Phase 2 starts withfinder = 0andslowsomewhere in the cycle; they still converge correctly to node0. - Tiny arrays (
n = 1withnext_node = [0]):slowandfastboth equal0before the while loop body executes (they start equal), so phase 2 immediately returns0.
Complexity
| Time | O(n) — both phases traverse at most O(n) steps |
| Space | O(1) — only a fixed number of integer pointers used |
from typing import List
def find_cycle_entry(next_node: List[int]) -> int:
# Phase 1: detect meeting point inside the cycle
slow = next_node[0]
fast = next_node[next_node[0]]
while slow != fast:
slow = next_node[slow]
fast = next_node[next_node[fast]]
# Phase 2: find cycle entry
finder = 0
while finder != slow:
finder = next_node[finder]
slow = next_node[slow]
return finder