Cycle Entry Point
A linked list is given as a list nxt, where nxt[i] is the index of the node after node i, or -1 for the end. The list starts at node 0. Return the index of the node where a cycle begins, or -1 if there is no cycle (or the list is empty).
Requirements
- Use extra space (Floyd's tortoise and hare).
Examples
EXAMPLE WALKTHROUGHcycle back to node 1
A guided view of the published example, not a trace of your code.
INPUT
[[1,2,3,1]]TASKcycle_entry_point()
EXPECTED OUTPUT
1Start with the input values shown in this example.
Example 1 of 3
Input: nxt = [1,2,3,1]
Output: 1
Explanation: cycle back to node 1
Input: nxt = [1,2,-1]
Output: -1
Explanation: no cycle
Input: nxt = [0]
Output: 0
Explanation: self loop
Hint 1
Move a slow pointer one step and a fast pointer two steps until they meet.
Hint 2
Then move one pointer back to the head; stepping both one at a time, they meet at the entry.