Algorithm:
- find the loop and record where slow and fast hit
- set slow = head
- start traverse again, until the pointers hit again
Analysis:
- When they first hit, fast is x steps ahead of slow, where x = k * len(loop).
- So in the second traverse, when slow first enter the loop, fast is exatcly k circles ahead and hit slow again.
Given a linked list, return the node where the cycle begins. If there is no cycle, return null
.
Note: Do not modify the linked list.
Follow up:
Can you solve it without using extra space?
Soulution:
1 | class Solution(object): |