dsa · easy

Linked List Cycle

GreyOrangeLinked ListTwo PointersFoundation

head is a list of node values. pos is the index the tail links back to, or -1 if there is no cycle. Reconstruct the list and return whether a cycle exists.

Arguments

**Example:** head = [3,2,0,-4], pos = 1true.

Constraints

0 <= len(head) <= 4*10^4, pos is -1 or a valid index Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.

Examples

Example 1

Input:
[3,2,0,-4]
1

Expected:
true

Example 2

Input:
[1,2]
-1

Expected:
false

Open in the Dojo editor