LeetCode Linked List Cycle II 和I 通用算法和优化算法
来源:互联网 发布:mysql创建索引的优缺点 编辑:程序博客网 时间:2024/05/29 13:24
Linked List Cycle II
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?
和问题一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;}};
更新I的O(n)算法:
//2014-2-19 updatebool 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;}
现在Leetcode又更新了,第一个程序已经是Time limit exceeded了。已经更新了快指针,慢指针解法。
更新 II的O(n)算法。快指针,慢指针的解法。
//2014-2-19 updateListNode *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的长度就会是绕圈几周了,但是结果也是一样成立的。
知道结论,并会推导就够了。
2014-7-5 Update:
既然有人说上面没能证明a很长,而圈很小的时候的情况,那么我就增加几句普通情况下的证明吧,就是不管圈的大小去证明吧:
1. 假设圈的周长L
2. 那么相遇的时候slow走:a + b,而fast走:a + b + n*L,(n代表fast走了多少圈)
3. fast走路的路程是slow的两倍,那么2(a+b) = a + b + n*L,得到a = n*L - b
4 从相遇点的时候开始,放一个指针从开始点走起,另一个指针继续走,而且这时走的速度都是一样的,那么当一个指针从开始点X走到循环圈点Y的时候,走了a路程,而另一个指针走的路程是n*L-b,那么两者的路程是一样的,相遇点必然是Y。
从而定理得到证明,而不管这个圈的大小。
这样的证明够严密了,哪里用得着那么啰嗦,什么圈大圈小的都可以不用管。圈大的时候,不过是n == 1的特殊情况。
难倒你还想问:如果n == 0的时候呢? 呵呵,仔细想想,n不可能等于0的。
- LeetCode Linked List Cycle II 和I 通用算法和优化算法
- leetcode题目:Linked List Cycle和Linked List Cycle II
- leetcode 141. Linked List Cycle和II
- leetcode Linked List Cycle I II
- LeetCode:Linked List Cycle I & II
- LeetCode :: Linked List Cycle I and II
- leetcode Linked List Cycle(I II)(*)
- leetcode-Linked List Cycle I & II
- LeetCode: linked list cycle I and II
- 【LeetCode】Linked List Cycle I&II
- LeetCode - Linked List Cycle I &II
- LeetCode | Linked List Cycle I,II
- LeetCode: Linked List Cycle I & II
- LeetCode Linked List Cycle II(Floyd 判圈算法)
- [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环
- [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环
- Linked List Cycle I和II 以及扩展
- LeetCode 141 Linked List Cycle和142 Linked List Cycle II
- 斜率小于0的连线数量-归并排序
- NineOldAndroidsDemos 学习(3) PathAnimationActivity
- WebService—快速实现一个基于JAX-WS的WebService项目(二)
- Documentation/vm/zswap.txt
- windows下zmq的编译
- LeetCode Linked List Cycle II 和I 通用算法和优化算法
- Oracle技术支持是如何分析数据库性能问题的
- 2013博客之星评选,如果喜欢我的文章,请投我一票
- 消息循环
- JSP中的requenst对象
- 空间目标瞬态温度特性研究
- WinForm二三事(四)界面布局(上)
- windows下jzmq编译中的常见问题
- HDU 1042 N!