Linked List Cycle
Given the head of a linked list, determine whether the list contains a cycle.
The test input is provided as a values array plus a pos index. If pos >= 0, the tail connects to the node at that index; if pos = -1, the list is acyclic.
Examples
Input: [[3,2,0,-4],1]
Output: true
Input: [[1,2],-1]
Output: false
Hints
If the list is empty or has only one node, return false immediately.
Initialize two pointers, `slow` and `fast`, both starting at the head. Move `slow` one step at a time and `fast` two steps at a time.
If `fast` reaches the end of the list (i.e., `fast` or `fast.next` is null), return false. If `slow` and `fast` meet at any point, return true indicating a cycle exists.
Related Problems
Linked List Cycle
Given the head of a linked list, determine whether the list contains a cycle.