129. Linked List Cycle II
MediumLinked List
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.
Examples
Input: [3,2,0,-4] 1
Output: 1
Explanation: The tail (-4) links back to the node at index 1 (value 2), so the cycle begins there.
Constraints
- The number of the nodes in the list is in the range [0, 10^4].
- -10^5 <= Node.val <= 10^5
- pos is -1 or a valid index in the linked-list.
Loading...
Run checks all cases above. Submit evaluates all test cases.