Linked List Cycle II
Question
Description
Given a linked list, return the node where the cycle begins.
If there is no cycle, returnnull.
Example
Given-21->10->4->5, tail connects to node index 1,return10
Challenge
Follow up:
Can you solve it without using extra space?
题解思路
可以先判断Linked List是否有Cycle,利用two pointers可以很快得到。接下来就要分析,快慢指针第一次相遇时,位置、步数和环的关系。
第一次相遇时,慢指针所走步数为
a + b
快指针走的步数为
a + b + nR
我们知道快指针是慢指针速度的2倍,因此
2(a + b) = a + b + nR
那么
a + b = nR
同时
b + c = R
所以
a = (n - 1)R + c;
也就是说,从A点和C点同时出发,以相同的速度前进,那么下一次相遇的位置将是B。
参考 LeetCode: https://leetcode.com/articles/linked-list-cycle/
源代码
Easier to understand:
Preferred Implementation:
Harder to understand with pointer corner cases
Last updated
Was this helpful?