Determine whether a linked list contains a cycle — i.e. some node can be reached again by following next pointers. Use O(1) memory.
Input
pos — the tail connects to the node at this 0-based index (-1 means no cycle). Your function receives only the head; the cycle is already wired.Output: true or false.
3 2 0 -4 1
true
Tail connects back to value 2.
1 2 0
true
1 -1
false
Run your code against the sample tests, or Submit to run the full suite.