Linked List Cycle
Return whether repeatedly following next from head eventually revisits a node. Input is {values,pos}; the last node points to zero-based pos, or null when pos is -1. Use constant auxiliary space.
Example 1
head = {"values":[3,5,7],"pos":1}trueExplanation
The last node links back to index 1, creating 5->7->5.
Example 2
head = {"values":[1],"pos":0}trueExplanation
The single node points to itself, so it forms a cycle.
Example 3
head = {"values":[],"pos":-1}falseExplanation
An empty list cannot revisit a node.
Example 4
head = {"values":[1,2],"pos":-1}falseExplanation
The last node ends at null, so the two-node list never revisits a node.
Constraints
- 0 <= number of nodes <= 10000
- pos is -1 or a valid index
- pos is encoding metadata and is not a function argument
Function contract
hasCycleArguments: cycleListSolutionRuns on your device
Loading draft…
Run: 3 examples; Submit: 35 cases, on your device.
{"values":[3,5,7],"pos":1}