LeetCode Linked List Cycle II 单链表环2 (找循环起点)

题意:给一个单链表,若其有环,返回环的开始处指针,若无环返回NULL。

思路:

  (1)依然用两个指针的追赶来判断是否有环。在确定有环了之后,指针1跑的路程是指针2的一半,而且他们曾经跑过一段重叠的路(即1跑过,2也跑过),就是那段(环开始处,相遇处),那么指针2开始到环开始处的距离与head到指针相遇处是等长的喔~,那么再跑一次每次一步的就必定会相遇啦。画个图图好方便看~

  (2)其实还有另一个直观的思路,就是指针1和2相遇后,p指向他们的next,在他们相遇处的next给置空,再跑一遍那个“找两个链表后半段重叠的开始处”那道题就行了。

(1)代码

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11         ListNode *detectCycle(ListNode *head) {
12             if(!head)   return 0;
13             ListNode *one=head, *two=head->next;
14             while(two&&two->next&&one!=two)
15             {
16                 one=one->next;
17                 two=two->next->next;
18             }
19             if(!two||!two->next) return 0;  //无环
20             two=two->next;//此时他们已经相遇了,two后移一步,使two与head同时到one等长。
21             while(head!=two)//必定会相遇
22             {
23                 head=head->next;
24                 two=two->next;
25             }
26             return head;
27         }
28 };

AC代码

(2)代码

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode *detectCycle(ListNode *head) {
12         if(!head)   return 0;
13         ListNode *one=head, *two=head->next;
14         while(two&&two->next&&one!=two)
15         {
16             one=one->next;
17             two=two->next->next;
18         }
19         if(!two||!two->next) return 0;  //无环
20         one=one->next;  //此时two还在断口处
21         two->next=0;
22
23         ListNode * p1=one, *p2=head;
24         while(p1 && p2 && p1!=p2 )//两链表找重叠处~即使p1p2到开始重叠处不等长也能解决
25         {
26             p1=p1->next;
27             p2=p2->next;
28
29             if(!p1) p1=head;
30             if(!p2) p2=one;
31         }
32         two->next=one;
33         return p1;
34     }
35 };

AC代码

时间: 2024-12-16 08:07:04

LeetCode Linked List Cycle II 单链表环2 (找循环起点)的相关文章

[LeetCode] 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? 这个求单链表中的环的起始点是之前那个判断单链表中是否有环的延伸,可参见我之前的一篇文章 (http://www.cnblogs.com/grandyang/p/4137187.html). 还是要设

[Leetcode] 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? 题意:给定链表,若是有环,则返回环开始的节点,没有则返回NULL 思路:题目分两步走,第一.判断是否有环,第二若是有,找到环的起始点.关于第一点,可以参考之前的博客 Linked list cycle.

LeetCode Linked List Cycle II

/** * 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* fast = head; ListNode* slow = head;

LeetCode: Linked List Cycle II [142]

[题目] 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? [题意] 给定一个单向链表,如果链表有环,则返回环开始的位置. [思路] 仍然是维护两个指针, p1, p2, p1每次走一步, p2每次走两步 假设进入环之前要走X步,环长为y步,p2第

[LeetCode]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? 从问题来看,如果可以充分利用额外空间的话,这个题目是不难的,然而题目提出了一个要求,能否在不使用任何额外空间的情况下解决这个问题. 通过反复思考,我觉得这题类似于追击问题,可以用一个

LeetCode: Linked List Cycle ii 解题报告

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? SOLUTION 1: 最开始的想法和 Linked List Cycle这一题一样. SOLUTION 2: 同样是两个指针,一快一慢,相遇时跳出循环,只

[LeetCode] 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? Hide Tags Linked List Two Pointers 开始犯二了一点,使用了node 的val 作为判断,其实是根据内存判断.找出链表环的起始位置,这个画一下慢慢找下规律便可以: 思路

[LeetCode]93. Linked List Cycle II查找链表中环的起始节点

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note: Do not modify the linked list. Follow up:Can you solve it without using extra space? Subscribe to see which companies asked this question 解法:这道题是题目L

[LeetCode] Linked List Cycle II, Solution

Question : 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? Anaylsis : 首先,比较直观的是,先使用Linked List Cycle I的办法,判断是否有cycle.如果有,则从头遍历节点,对于每一个节点,查询是否在环里面,是