Easy · Linked Lists

Linked list cycle

Given the head of a singly linked list, return true if following next pointers ever reaches the same node twice (the list has a cycle) and false if the walk ends at null. 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

{
  "values": [3, 2, 0, 4],
  "loopAt": 1
}

Output: true

Example 2

{
  "values": [1, 2, 3, 4, 5],
  "loopAt": -1
}

Output: false

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