Medium · Linked Lists

Detect loop

Given the head of a singly linked list, return the node where its cycle begins (the first node that following next pointers reaches twice), or null if the list has no cycle. In the samples, loopAt is the 0-based index the tail links back to, or −1 when the tail ends in null.

Examples

Example 1

[1,2,3,4,5] ↺ idx 1

Output: loop entry @ 1 (val 2)

Example 2

[1,2,3,4] no loop

Output: no loop

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Linked Lists problems