Linked List Cycle II
Given the head of a linked list, return the node where the cycle begins. If there is no cycle, return null. You must not modify the list, and should aim for O(1) extra memory.
Open official problem prompt ↗Locate the exact node where a cycle starts (or prove none exists) using constant extra memory and without altering the list.
Two runners on a looping track, one twice as fast; they inevitably meet, and a simple distance argument then walks you back to where the loop joins the straightaway.
- Input
- head = [3,2,0,-4], pos = 1 (the tail's next points to the node at index 1)
- Output
- Node with value 2 (the node at index 1)
- Why
- The tail -4 links back to the node valued 2, so the cycle begins at that node.
The number of nodes is in the range [0, 10^4]-10^5 <= Node.val <= 10^5pos is -1 or a valid index into the listpos is not passed as a parameter; it only describes the test structure