Given head of a linked list, determine if the linked list has a cycle. Input: head array and pos (index where tail connects to). pos = -1 means no cycle.
Example 1
Input: head = [3,2,0,-4,5,6], pos = 2
Output: true
Explanation: The tail node (6) points back to the node at index 2 (value 0), so following next pointers loops forever. There is a cycle.
Example 2
Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: The tail node (-4) points back to the node at index 1 (value 2), creating a cycle.
Example 3
Input: head = [1,2], pos = 0
Output: true
Explanation: The tail node (2) points back to the head, creating a cycle of two nodes.
Send two pointers walking down the list — `slow` taking 1 step at a time, `fast` taking 2. On a straight list, `fast` falls off the end. On a cyclic list, both eventually enter the cycle, and from that moment `fast` gains 1 position on `slow` each iteration — so they're guaranteed to meet.
Run checks all cases above. Submit evaluates all test cases.