Skip to interview prep
Apexed

86. Find Where a Cycle Begins

MediumLinked lists

Return the 0-based index of the node where a cycle in the linked list begins, or -1 if the list has no cycle. Use O(1) extra memory.

The tests write the list as { values, pos }: the tail links to the node at index pos, or to nothing when pos is -1. Your function only receives the head.

Example 1

Input: values = [3, 2, 0, -4], pos = 1
Output: 1

Constraints

  • 0 ≤ nodes ≤ 10⁴

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 5 hidden and sample tests.

Code runs only in your browser in an isolated sandbox. Progress and submissions are saved on this device.

Find Where a Cycle Begins — coding interview practice — Apexed