题目如下:
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?
题目分析:
本题是上一道题目的扩展。可以沿用上一题目的方式。使用一块一慢两个指针从起点开始行走。快指针每次走2步,慢指针每次走1步。如果链表中有环,2者必然会在环中某点相遇。
假设链表恰好是尾巴指向了开头,即假设链表恰好从头道尾都是一个环。那么相遇点必然在两个指针开始走动的起点。
假设链表的组成为先有一段单链表后有一个环,那么相遇点显然不一定是起点了,相遇点显然也和之前那段单链表的长度是相关的。
具体的数学关系可以通过推导得知,参考了这篇博客,如下。
1 设链表长度为len(链表中非空next指针的个数,下面所说的长度均为非空next指针的个数),链表head到环的起点长度为a,环起点到快慢指针相遇点的长度为b,环的长度为r。
2 假设到快慢指针相遇时,慢指针移动的长度为s,则快指针移动长度为2s,而快指针移动的长度还等于s加上在环上绕的k圈(k>=1),所以2s=s+kr ,即s = kr。
3 由s = a + b 和 s = kr 可知 a + b = kr = (k-1)r + r; 而r = len - a,所以a + b = (k-1)r + len - a, 即 a = (k-1)r + len - a - b,len - a - b是相遇点到环的起点的长度,由此可知,从链表头到环起点长度 = (k-1)环长度+从相遇点到环起点长度,于是我们从链表头、与相遇点分别设一个指针,每次各走一步,两个指针必定相遇,且相遇点为环起点。
时间复杂度为O(N),代码是
//56ms过大集合
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode* first=head;
ListNode* second=head;
if(head==NULL||head->next==NULL)
//注意分析临界情况 (1)成环单节点; (2)不成环单节点; (3)成环2节点; (4)不成环2节点
return NULL;
while(first!=NULL&&second!=NULL&&second->next!=NULL){ //判断是否成环
first=first->next;
second=second->next->next;
if(first==second)
break;
}
if(first==second){
first=head;
while(first!=second&&second!=NULL&&second->next!=NULL){ //找到环的起点
first=first->next;
second=second->next;
if(first==second)
break;
}
return first;
}else{
return NULL;
}
}
};
在这之前,由于没有找到a和b的数学关系。。。。我写了另外一个版本,时间复杂度为O(N²)过大集合悲催地超时了。。。。
//超时
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode* out_first=head;
ListNode* in_first=head;
ListNode* in_second=head;
if(head==NULL||head->next==NULL)
return NULL;
while(out_first!=NULL) { //遍历单链表,以当前节点作为快慢指针进行追赶过程的起点。
in_first = out_first;
in_second= out_first;
while(in_first!=NULL&&in_second!=NULL&&in_second->next!=NULL) { //开始进行快慢指针的追赶过程
in_first=in_first->next;
in_second=in_second->next->next;
if(in_first==in_second&&in_first==out_first) //判断相遇点是否是起点
return out_first;
else if(in_first==in_second&&in_first!=out_first)
break;
}
out_first=out_first->next;
}
return NULL;
}
};
update: 2014-12-30
我之前比较纠结,在有环发生追及的情况下,追及点A到底是在环的入口的上方(图中A点)还是下方(图中B点), 后来发现,不管是A点还是B点,都可以得到数学表达式L1 = L3.
如上图所示
//60ms
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode* fast = head;
ListNode* slow = head;
// bool has_cycle = false; //这个bool变量没有必要使用,可以通过fast == NULL来判断是否有环。
while (slow != NULL && fast != NULL) {
//注意这个条件的写法,不能省略。一开始错写为 while (slow != NULL)。注意slow和fast差距越来越大,所以需要同时判断slow和fast
slow = slow->next;
fast = fast->next;
if(fast != NULL)
fast = fast->next;
if (fast == slow)
break;
}
if (fast == NULL) return NULL;
slow = head;
while (slow != fast) {
slow = slow->next;
fast = fast->next;
}
return fast;
}
};