LeetCode 142. Linked List Cycle II
来源:互联网 发布:mac炉石传说无法运行 编辑:程序博客网 时间:2024/05/17 07:54
描述
找到环的起始节点
解决
若存在环,则从头节点和相遇点每次走一步,一定会在起始点处相遇。
/** * 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) { if (!head) return false; ListNode *t1 = head, *t2 = head; while (t2 -> next && t2 -> next -> next) { t1 = t1 -> next; t2 = t2 -> next -> next; if (t1 == t2) { t2 = head; while (t1 != t2) { t1 = t1 -> next; t2 = t2 -> next; } return t1; } } return NULL; }};
0 0
- [LeetCode]141. Linked List Cycle【&142. Linked List Cycle II】
- leetcode--141. Linked List Cycle&&142. Linked List Cycle II
- LeetCode 141. Linked List Cycle && 142. Linked List Cycle II
- 142. Linked List Cycle II leetcode list
- LeetCode 142. Linked List Cycle II
- [LeetCode]142.Linked List Cycle II
- [leetcode] 142.Linked List Cycle II
- [leetcode] 142. Linked List Cycle II
- leetcode 142. Linked List Cycle II
- [Leetcode]142. Linked List Cycle II @python
- [LeetCode]142. Linked List Cycle II
- 142. Linked List Cycle II LeetCode
- LeetCode:142. Linked List Cycle II
- LeetCode--142. Linked List Cycle II
- LeetCode 142. Linked List Cycle II
- LeetCode 142. Linked List Cycle II
- LeetCode *** 142. Linked List Cycle II
- LeetCode 142. Linked List Cycle II
- Android序列化的存储和读取
- hibernate无法自动建表问题
- java之断言assert
- 位向量实现的bitset集合
- JSP 动作元素 标签
- LeetCode 142. Linked List Cycle II
- XGBoost 脚本学习
- C++为什么remove以后需要erase
- jsonp原理
- POJ1068
- iOS中收起键盘的方法和时机
- matlab 交易建模
- 用node实现http服务器 三
- 关于a different object with the same identifier value was already associated with the session解决方案