LeetCode[Linked List]: 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?

这个题目跟Linked List Cycle一样,我也没有能够自己独立找到解决方法,而是借助Discuss学到了一个非常巧妙的解决方法:

首先借助LeetCode[Linked List]: Linked List Cycle同样的方法判断是否存在环,如果存在环:

假设当fast指针和slow指针相遇时已经走了k步,又设环的长度为r,则:2k-k=nr => k=nr。也就是说已经走的步数k一定是环的整数倍,假设链表起始点与环起始点的距离为m,那么从相聚点再走m步必然到达环起始点。所以将slow重置为头结点,fast不变,仍然是相聚点,这次两个指针每次都移动一步,那么两者再次相遇时必然就是环的起始点。

以上是我根据Discuss上的观点,自己得到的解释。原文是这样解释的:

my solution is like this: using two pointers, one of them one step at a time. another pointer each take two steps. Suppose the first meet at step k,the length of the Cycle is r. so..2k-k=nr,k=nr Now, the distance between the start node of list and the start
node of cycle is s. the distance between the start of list and the first meeting node is k(the pointer which wake one step at a time waked k steps).the distance between the start node of cycle and the first meeting node is m, so...s=k-m, s=nr-m=(n-1)r+(r-m),here
we takes n = 1..so, using one pointer start from the start node of list, another pointer start from the first meeting node, all of them wake one step at a time, the first time they meeting each other is the start of the cycle.

代码实现非常简单:

ListNode *detectCycle(ListNode *head) {
    if (!head) return false;

    ListNode *slow = head, *fast = head;
    while (slow->next && fast->next && fast->next->next)
    {
        slow = slow->next;
        fast = fast->next->next;
        if (slow == fast) {
            for (slow = head; slow != fast; slow = slow->next, fast = fast->next) ;
            return slow;
        }
    }

    return NULL;
}
时间: 2024-08-28 18:13:28

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

leetcode第一刷_Linked List Cycle II

这道题稍微有点意思,知道答案发现,呀,这么简单就能做啊.我一开始想的是,相遇之后用另一个指针怎么走,然后满足什么关系之后能推出来,其实不用这么麻烦.是很简单的数学关系,我画个图说一下. S1代表的是链表进入环之前的长度,a代表当两个指针相遇时,走一步的指针在环里走的长度,S2代表的是环的周长,那么根据条件,相遇时,走两步的指针走的距离是走一步的两倍,我们得到公式: (S1+a)*2 = S1+S2+a 化简一下得到 S1 = S2-a 即,环中剩下的长度刚好等于进入链表之前的长度.于是解法是:当

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? 题意: 给定一个链表,找到环起始的位置.如果环不存在,返回NULL. 分析: (1)首先要判断该链表是否有环.如果没有环,那么返回NULL. (2)其次,当已知环存在后,寻找环起始的位置. 思路: (

[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

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:142. Linked List Cycle II(Java)解答

转载请注明出处:z_zhaojun的博客 原文地址:http://blog.csdn.net/u012975705/article/details/50412899 题目地址:https://leetcode.com/problems/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

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刷题笔记】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? 判断一个链表是否有环. 题解: 设置两个指针p1和p2: p1每次走一步,p2每次走两步,如果在这个过程中,p2为空,则没有环:否则两个指针必然相遇,则有环: 接下来找环的起点,将p1挪动到链表起始,