您的位置:首页 > 其它

Linked List Cycle II

2014-03-22 00:17 127 查看
Given a linked list, return the node where the cycle begins. If there is no cycle, return
null
.

Follow up:
Can you solve it without using extra space?

思路:这道题有点难度,需要用到数学推理。如下图,需要设置慢指针slow,一次走一步和快指针fast,一次走两步,我们设环开始位置距离头结点的距离为K,当快指针追到慢指针时候的位置设为慢指针走的距离为x,快指针走的距离为y。链表的总长度为L。那么我们可以有如下推算过程:

y=2x;

y=L+x-k;这样可以退出k=L-x;也就是图上黑色部分的长度等于红色部分的长度。这样我们就可以看出后面的求解过程了。

快指针和慢指针重合的地方开始,慢指针继续行走一次一步,快指针从头结点开始重新来过,但是这次一次就要走一步了,等慢指针和快指针重合的地方就是环开始的位置。



/**
* Definition for singly-linked list.
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode *slow,*fast;
fast=head;
if(fast==NULL)
return NULL;
fast=fast->next;
slow=fast;
if(fast==NULL)
return NULL;
fast=fast->next;
while(fast!=NULL && fast!=slow)
{
fast=fast->next;
slow=slow->next;
if(fast==NULL)
return NULL;
fast=fast->next;
}
if(fast==NULL)
return NULL;
fast=head;
while(fast!=slow)
{
fast=fast->next;
slow=slow->next;
}
return fast;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: