[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?

题意:

给定一个链表,找到环起始的位置。如果环不存在,返回NULL。

分析:

(1)首先要判断该链表是否有环。如果没有环,那么返回NULL。

(2)其次,当已知环存在后,寻找环起始的位置。

思路:

(1)链表是否有环,利用快慢指针很好解决。

(2)当环存在时,寻找环的起始位置:

++++使用额外的空间,可以用一个map存放链表的节点,遍历链表:

如果当前节点在map中没有出现,那么将该节点加入到map中,否则,返回当前节点(也就是环的起点)。

-----不使用额外空间,利用快慢指针的特性。

1st ruond:
SP(start point)                               MP(match point)
A: --------------------------------------------------------MP
B: - - - - - - - - - - - - - MP

2nd round:
B:                           MP- - - - - -- - - - - - - - -MP
C:---------------------------------------------------------MP
3rd round:
B1:- - - - - - - - - - - - - MP
B2:                          MP- - - - - - - - - - - - - - MP

B1与B2必定会在途中相遇。 那么,他们首次相遇的点即为环的起始点。

设定两个指针A,B都从SP(Start point)出发: A每次向后移动两个节点,B每次向后移动一个节点。

假设A,B在MP(match point)相遇。 那么A所走的路径长度为B所走的路径长度的2倍:La = 2*Lb。

假设有另一个指针C还是从SP启动,每次向后移动两个节点, B从MP开始出发每次向后移动一个节点,那么可以想象C和B会在MP相遇。

如果C从SP出发,每次只移动一个节点,B依旧从MP出发且每次向后移动一个节点, 那么B和C依旧会在MP相遇。

class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        // list length less than two
        if(head==NULL || head->next==NULL) return NULL;
        // if cycle exists in the list
        ListNode *startA= head->next;
        ListNode *endA= head->next->next;
        while(endA!=NULL && endA != startA){
            endA=endA->next;
            if(endA){
                endA=endA->next;
                startA=startA->next;
            }
        }
        // no cycle exists
        if(endA==NULL) return NULL;
        // find a cycle then search the start point
        ListNode *match = startA;
        ListNode *start = head;
        while(start!=match){
            start = start->next;
            match = match->next;
        }
        return start;
    }
};

转载请注明出处: http://www.cnblogs.com/double-win/ 谢谢!

[LeetCode 题解]: Linked List Cycle II,布布扣,bubuko.com

时间: 2024-08-27 15:11:07

[LeetCode 题解]: Linked List Cycle II的相关文章

【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可知,利用一快一慢两个指针能够判断出链表是否存在环路.假设两个指针相遇之前slow走了s步,则fast走了2s步,并且fast已经在长度

LeetCode OJ - 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? 解题思路: 设置两个指针slow和fast,从head开始,slow一次一步,fast一次两步,如果fast能再次追上slow则有圈. 设slow走了n步,则fast走了2*n步,设圈长度m

[LeetCode][JavaScript]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. Note: Do not modify the linked list. Follow up:Can you solve it without using extra space? https://leetcode.com/problems/linked-list-

Java for LeetCode 142 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 JAVA实现如下: public ListNode detectCycle(Li

Leetcode 链表 Linked List Cycle II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Linked List Cycle II Total Accepted: 20444 Total Submissions: 66195My Submissions Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you

Leetcode 142 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 141 Linked List Cycle 性质:distance from head to 环开始点 == distance from 双指针相遇的点 to 环开始点 证明: 指

[C++]LeetCode: 73 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: 73 Linked List Cycle. 使用快慢两个指针,首先先判断链表是否存在环,如果存在,从相遇点开始,还有另外一个从链表头开始,同时移动一步,

[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 (middle)

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? 思路: 做过,就当复习了. 先用快慢指针判断相交,关键是环开始点的获取. 用上图说明一下,设非环的部分长度为a(包括环的入口点), 环的长度为b(包括环的入口点). 快慢指针相交的位置为绿色的点,距离