Given the head of a linked list, return the node where the cycle begins. If there is no cycle, return null. There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that the tail's next pointer connects to (0-indexed). It is -1 if there is no cycle. Note that pos is not passed as a parameter. Do not modify the linked list. Return the index of the node where the cycle begins, or -1 if there is no cycle.
Example 1
Input: head = [3,2,0,-4], pos = 1
Output: 1
Explanation: The tail (-4) links back to the node at index 1 (value 2), so the cycle begins there.
Example 2
Input: head = [1,2], pos = 0
Output: 0
Explanation: The tail links back to the head; the cycle begins at index 0.
Example 3
Input: head = [1], pos = -1
Output: -1
Explanation: A single node with no cycle.
A slow and a fast pointer meet inside the cycle. From the meeting point and from the head, two pointers advancing one step at a time meet exactly at the cycle's entrance.
Run checks all cases above. Submit evaluates all test cases.