LeetCode Linked List Cycle II
来源:互联网 发布:数据分析培训班哪个好 编辑:程序博客网 时间:2024/05/10 01:46
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?
struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; class Solution {public: ListNode *detectCycle(ListNode *head) { bool flag = false; ListNode *fast, *slow; fast = slow = head; while(fast && fast->next) { fast = fast->next->next; slow = slow->next; if(fast == slow) { flag = true; break; } } if(!flag) return NULL; int count = 1;// count表示环的长度 fast = fast->next->next; slow = slow->next; //当fast与slow重合后,还需count-1次才能重新重合 while(fast != slow) { fast = fast->next->next; slow = slow->next; count++; } fast = slow = head; while(count--) fast = fast->next;while(fast != slow) {fast = fast->next;slow = slow->next;}return fast; }};
0 0
- 【LeetCode】 Linked List Cycle II
- Leetcode: Linked List Cycle II
- <Leetcode>Linked List Cycle II
- [LeetCode] Linked List Cycle II
- Leetcode: Linked List Cycle II
- LeetCode:Linked List Cycle II
- Leetcode Linked List Cycle II
- [LeetCode]Linked List Cycle II
- leetcode Linked List Cycle II
- LeetCode | Linked List Cycle II
- Leetcode: Linked List Cycle II
- [LeetCode] - Linked List Cycle II
- Leetcode: Linked List Cycle II
- LeetCode - Linked List Cycle II
- leetcode Linked List Cycle II
- Linked List Cycle II -- LeetCode
- LeetCode Linked List Cycle II
- [LeetCode] Linked List Cycle II
- 关于“Android SDK manager中不出现完整Android版本安装包列表”的动态解决方法
- WinDbg命令详解--远程调试
- 美国大学课堂的契约精神(纽约时报中文网 )
- 我阿尼尼南宁
- 20140317孤独者之旅29
- LeetCode Linked List Cycle II
- Spring Framework 4.0相关计划公布---包括对于Java SE 8 和Groovy2的支持
- WinDbg命令详解--内存操作
- 蝴蝶效应
- java ftp 文件上传下载
- fatal error C1083: 无法打开预编译头文件 的解决方法
- node.js一些认识
- 物理Data Guard角色转换步骤(switch over)
- WinDbg命令详解--其他命令