Description
Given a linked list with potential cycle (last element points to index), return true if cycle exists.
Examples
Input:
head = [3,2,0,-4], pos = 1Output:
trueExplanation:
Tail connects to node at index 1.
Input:
head = [1,2], pos = 0Output:
trueExplanation:
Node 2's next pointer connects back to node 1 at index 0, forming a cycle.
Input:
head = [1], pos = -1Output:
falseExplanation:
A single node with no cycle (pos = -1) has no next pointer looping back.
Constraints
- •
0 ≤ list length ≤ 10⁴