Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull
.
Follow up:
Can you solve it without using extra space?
和问题一Linked List Cycle几乎一样。如果用我的之前的解法的话,可以很小修改就可以实现这道算法了。但是如果问题一用优化了的解法的话,那么就不适用于这里了。下面是我给出的解法,可以看得出,这里需要修改很小地方就可以了。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool find(ListNode *head, ListNode *testpNode)
{
ListNode *p = head;
while (p != testpNode->next)
{
if(p == testpNode)
return false;
p = p->next;
}
return true;
}
ListNode *detectCycle(ListNode *head) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if(head == NULL)
return NULL;
ListNode *cur = head;
while(cur != NULL)
{
if(find(head, cur))
return cur->next;
cur = cur->next;
}
return NULL;
}
};
然后转一下下面那位朋友的博客,他的解法很优化,不过只适合第一个LeetCode Linked List Cycle问题,而不适合这里。值得学习学习,一起贴在这里了。
http://blog.csdn.net/doc_sgl/article/details/13614853
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool hasCycle(ListNode *head) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
ListNode* pfast = head;
ListNode* pslow = head;
do{
if(pfast!=NULL)
pfast=pfast->next;
if(pfast!=NULL)
pfast=pfast->next;
if(pfast==NULL)
return false;
pslow = pslow->next;
}while(pfast != pslow);
return true;
}
};
更新I的O(n)算法:
//2014-2-19 update
bool hasCycle(ListNode *head)
{
ListNode *fast = head;
ListNode *slow = head;
while (fast)
{
slow = slow->next;
fast = fast->next;
if (fast) fast = fast->next;
if (fast && slow == fast) return true;
}
return false;
}
更新 II的O(n)算法。快指针,慢指针的解法。
//2014-2-19 update
ListNode *detectCycle(ListNode *head)
{
if (!head || !head->next) return nullptr;
ListNode *slow = head->next;
ListNode *fast = head->next->next;
while (fast && fast != slow)
{
slow = slow->next;
fast = fast->next? fast->next->next:fast->next;
}
if (!fast) return nullptr;
for (fast = head; fast != slow; fast = fast->next) slow = slow->next;
return slow;
}
参考这两个博客的图,和一两句话就够了。http://www.cnblogs.com/hiddenfox/p/3408931.html
http://blog.csdn.net/cs_guoxiaozhu/article/details/14209743
图:
设:链表头是X,环的第一个节点是Y,slow和fast第一次的交点是Z。各段的长度分别是a,b,c,如图所示。环的长度是L。slow和fast的速度分别是qs,qf。
第一次相遇时slow走过的距离:a+b,fast走过的距离:a+b+c+b。
因为fast的速度是slow的两倍,所以fast走的距离是slow的两倍,有 2(a+b) = a+b+c+b,可以得到a=c(这个结论很重要!)。
如果圈很小,而a很长,那么b的长度就会是绕圈几周了,但是结果也是一样成立的。
知道结论,并会推导就够了。
分享到:
相关推荐
算法 Leetcode刷题手册 labuladong的算法小抄官方完整版
这是LeetCode中Linked List所有题目的参考代码。(截止到目前为止:2015年12月14日)。
用来记录我们刷LeetCode题目时候的心酸历史。我们保证,这些代码一定通过了当时LeetCode的测试,虽然后续可能因为LeetCode测试条件的改变导致某些解题无法通过,但我们会实时的跟进。 编程语言使用Golang,代码风格...
文件中包含了LeetCode中Tag为LinkedList的题目参考代码。
leetcode常用算法java代码
leetcode中文版 LeetCode/Cpp 本人刷题记录在此,包含题意理解与算法思路,包含在Cpp文件内部注释,后续会持续更新。 有不懂的可以联系ji648513181,同时也欢迎志同道合O的朋友一起合作更新。 已更新剑指Offer答案...
LeetCode算法刷题
Java算法刷题带注释Leetcode,基础算法
leetcode初级算法代码C++完整版,里面是我认为最适合于新手解题的代码,包含详细的注释,还有个人感悟和总结,适合于想要入门的新手。
2020高频面试算法整理 leetcode ,18个大类,80+到常见算法题。 1.热身题|1)查找唯一数字|2)查找N/2数字|3)判断数字是否存在|4)合并二叉树|5)泡鸡蛋问题|2.互联网公司最常见的面试算法题有哪些?|3.TOP INTERVIEW ...
C语言 C语言 LeetCode算法刷题30天全面提升教程算法刷题30天全面提升教程
学习算法,LeetCode刷题,建议经典书籍《算法导论》《算法4》,使用C++和Python语言实现算法,提高算法效
LeetCode全部题目和详细解答 数据结构算法 程序员面试求职必备
leetcode题目精选,JAVA算法刷题,算法题leetcode题目精选,JAVA算法刷题,算法题leetcode题目精选,JAVA算法刷题,算法题leetcode题目精选,JAVA算法刷题,算法题leetcode题目精选,JAVA算法刷题,算法题leetcode...
leetcode算法。本书包含了 LeetCode Online Judge(http://leetcode.com/onlinejudge) 所有题目的答案,所有 代码经过精心编写,编码规范良好,适合读者反复揣摩,模仿,甚至在纸上默写。 全书的代码,使用 C++ 11 的...
基于java的leetcode刷题与复习指南算法模板代码
LeetCode 101_C++_算法_leetcode_leetcode101_leetcode101.zip
LeetCode 101_C++_算法_leetcode_leetcode101_leetcode101_源码.zip