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
head— the linked list, given as a Python list of node valuespos— 0-based index the tail links back to, or-1if there is no cycle
**Example:** head = [3,2,0,-4], pos = 1 → true.
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