1. 程式人生 > >020-142-Linked List Cycle II 判斷連結串列是否有環並返回環的起點

020-142-Linked List Cycle II 判斷連結串列是否有環並返回環的起點

Problem

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?

Solution

Actually there is a famous algorithm for this: Floyd’s cycle detection algorithm, also known as the hare-tortoise algorithm.

Floyd演算法用以判斷是否有環且找到環的節點。參考 。演算法如下:
0. 以快慢指標的方法先判斷是否有環,令慢指標一次走一步,快指標一次走兩步。假設連結串列有環,且快慢指標在某一點相遇。
1. 令連結串列起點到環起點的距離為m
2. 快慢指標相遇點到環起點的距離為k
3. 設環的長度為n,令a和b分別為慢指標和快指標相遇前在環內走的圈數。
4. 慢指標到相遇點的行走的距離為i = m + a*n + k,
5. 快指標到相遇點行走的距離為2i = m + b*n + k,
6. i = 2i - i = (b - a)*n 由這個公式可以表明i為環長度的倍數
7. 那麼 i = (m + k) + a*n,m + k 等於環長度的倍數,也就是說快指標距離環起點的距離是m
8. 由 7

的公式可以得到,令慢指標一步一步從連結串列頭部開始走,快指標從相遇點開始走,他們一定會在環起點相遇(走m步)。
由上演算法得到程式碼如下:

/**
 * 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) {
        bool is_circle = false
; ListNode* slow = head; ListNode* fast = head; while(slow != NULL && fast != NULL){ slow = slow->next; if(fast->next == NULL){ break; }else{ fast = fast->next->next; } if(slow == fast){ is_circle = true; break; } } if(is_circle){ slow = head; while(slow != fast){ slow = slow->next; fast = fast->next; } return slow; }else{ return NULL; } } };

當m=0時,即系連結串列的起點為環的起點時,即系 i = m + a*n + k = a*n + k, k為n的倍數,就是說明相遇點就是環的起點。

More

如何判斷環的長度?判斷連結串列有環後,快指標不動,慢指標從環相遇點開始再走一圈直到回到相遇點,所得步數即系環的長度。

相關推薦

020-142-Linked List Cycle II 判斷連結串列是否返回起點

Problem Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note: Do not modify the l

142. Linked List Cycle II(環形連結串列2,找到的入口點)

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. Fol

142. Linked List Cycle II連結串列

https://leetcode.com/problems/linked-list-cycle-ii/description/ 題目:如果連結串列有環,返回環的入口,負責返回NULL. 思路:快慢指標,考慮下面的連結串列環,其中4->2表示4的下一元素為2。 1->

[Leetcode-142] 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.

LeetCode | Linked List Cycle判斷連結串列是否

Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 題目解析: 判斷是否有環,只需要快慢指標即可。

[LeetCode-141] Linked List Cycle判斷連結串列是否

Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 【分析】 由於每一個父親只有可能有一個孩子,

LeetCode:141,Linked List Cycle判斷連結串列中是否

Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space?  方法1(利用hash表的方式): public b

【LeetCode】Linked List Cycle II(環形連結串列 II)

這是LeetCode裡的第142道題。 題目要求: 給定一個連結串列,返回連結串列開始入環的第一個節點。 如果連結串列無環,則返回 null。 說明:不允許修改給定的連結串列。 進階:你是否可以不用額外空間解決此題? 起初我在做這道題的時候,

【LeetCode-面試演算法經典-Java實現】【142-Linked List Cycle II(單鏈表中II)】

原題   Given a linked list, return the node where the cycle begins. If there is no cycle, retu

【演算法分析】如何理解快慢指標?判斷linked list中是否、找到的起始節點位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 為例Python實現

快慢指標簡述 快慢指標經常用於連結串列(linked list)中環(Cycle)相關的問題。 快指標(fast pointer)和慢指標(slow pointer)都從連結串列的head出發。 slow pointer每次移動一格,而快指標每次移動兩格。 如果快慢指標能相遇,則證明連結串列中有環;否則沒有

142. Linked List Cycle II

fast pro http spa node itl fad class 起點 題目: Given a linked list, return the node where the cycle begins. If there is no cycle, retur

LeetCode 142. Linked List Cycle II

linked blank code margin following ucs follow etc ref c臣8賦oe棧綠4敲ghttp://www.facebolw.com/space/2105094/following 擻r2灸6u186嫉雲磷俳8http://ww

142 Linked List Cycle II 環形鏈表 II

OS return leetcode 開始 ext 修改 cpp urn scrip 給一個鏈表,返回鏈表開始入環的第一個節點。 如果鏈表無環,則返回 null。說明:不應修改給定的鏈表。補充:你是否可以不用額外空間解決此題?詳見:https://leetcode.com/

leetcode-142 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. 想法:(1)首先的判斷連結串列中是否有環,若

LeetCode:142. 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 lis

LeetCode142. Linked List Cycle||(環形連結串列)——— 判斷連結串列是否以及求其入節點

struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(nullptr) {} }; //判斷連結串列是否有環 //方法: //1、空間複雜度n:使用set集合儲存每一個節點,判斷是否有重複 //2

[leetcode]142. Linked List Cycle II

 第一種解法:hash表 //hash表形式,最高只能到第二梯隊 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; *

LeetCode 142 Linked List Cycle II

turn problem desc temp esc link pro 一個點 ref LeetCode 142 每遍歷一個點,都要判斷起點到這個點的距離,和啟動點到這個點的next的距離。再比較一下就可以了。 class Solution { public: Li

LeetCode-142. Linked List Cycle II(詳細證明)

這道題目是141. Linked List Cycle的加成版https://leetcode.com/problems/linked-list-cycle/,思路都是“龜兔賽跑追及問題必相遇”,但這條需要確定環的入口節點。我們需要定量化一下:      

python leetcode 142. Linked List Cycle II

先判斷是否是環。假設環長為L,不是環的長度為M,在環中的N處相遇。那麼fast走了M+K1L+N,slow走了M+K2L+N。fast=2slow,M+K1L+N=2*(M+K2*L+N),N=(K1-K2)*L-M。可以看到從N出發再走M就到了環的起始點。 class Solution