Linked List Cycle II
2014-03-22 00:17
127 查看
Given a linked list, return the node where the cycle begins. If there is no cycle, return
Follow up:
Can you solve it without using extra space?
思路:这道题有点难度,需要用到数学推理。如下图,需要设置慢指针slow,一次走一步和快指针fast,一次走两步,我们设环开始位置距离头结点的距离为K,当快指针追到慢指针时候的位置设为慢指针走的距离为x,快指针走的距离为y。链表的总长度为L。那么我们可以有如下推算过程:
y=2x;
y=L+x-k;这样可以退出k=L-x;也就是图上黑色部分的长度等于红色部分的长度。这样我们就可以看出后面的求解过程了。
快指针和慢指针重合的地方开始,慢指针继续行走一次一步,快指针从头结点开始重新来过,但是这次一次就要走一步了,等慢指针和快指针重合的地方就是环开始的位置。
null.
Follow up:
Can you solve it without using extra space?
思路:这道题有点难度,需要用到数学推理。如下图,需要设置慢指针slow,一次走一步和快指针fast,一次走两步,我们设环开始位置距离头结点的距离为K,当快指针追到慢指针时候的位置设为慢指针走的距离为x,快指针走的距离为y。链表的总长度为L。那么我们可以有如下推算过程:
y=2x;
y=L+x-k;这样可以退出k=L-x;也就是图上黑色部分的长度等于红色部分的长度。这样我们就可以看出后面的求解过程了。
快指针和慢指针重合的地方开始,慢指针继续行走一次一步,快指针从头结点开始重新来过,但是这次一次就要走一步了,等慢指针和快指针重合的地方就是环开始的位置。
/** * 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) { ListNode *slow,*fast; fast=head; if(fast==NULL) return NULL; fast=fast->next; slow=fast; if(fast==NULL) return NULL; fast=fast->next; while(fast!=NULL && fast!=slow) { fast=fast->next; slow=slow->next; if(fast==NULL) return NULL; fast=fast->next; } if(fast==NULL) return NULL; fast=head; while(fast!=slow) { fast=fast->next; slow=slow->next; } return fast; } };
相关文章推荐
- 关于Linked List Cycle II (leetcode 142) 的几点思考
- LeetCode:Linked List Cycle II
- [leedcode 142] Linked List Cycle II
- linked-list-cycle-ii
- LeetCode Linked List Cycle II(找到带环单向链表的环起始位置)
- [LeetCode 第11题] -- Linked List Cycle II
- 【LeetCode练习题】Linked List Cycle II
- linked-list-cycle-ii (链表判环 并返回交点)
- Linked List Cycle II
- [leetcode]Linked List Cycle II
- Linked List Cycle II
- linked-list-cycle-ii(有环链表的第一个公共节点)
- CODE 139: Linked List Cycle II
- 142. Linked List Cycle II
- Linked List Cycle II
- leetcode-Linked List Cycle I & II
- [leetcode][list][two pointers] Linked List Cycle II
- Linked List Cycle II
- Linked List Cycle II
- leetcode_c++:链表:Linked List Cycle II(142)