1. 程式人生 > >[LeedCode OJ]#142 Linked List Cycle II

[LeedCode OJ]#142 Linked List Cycle II

【 宣告:版權所有,轉載請標明出處,請勿用於商業用途。  聯絡信箱:[email protected]

題意:

對於一個連結串列,判斷其是否有環,有環則返回環的起始位置。

思路:

通過141題,我們知道可以通過快慢指標來判斷是否有環,現在我們假設兩個指標相遇在z點,如圖


那麼我們可以知道fast指標走過a+b+c+b

slow指標走過a+b

那麼2*(a+b) = a+b+c+b

所以a = c

那麼此時讓slow回到起點,fast依然停在z,兩個同時開始走,一次走一步

那麼它們最終會相遇在y點,正是環的起始點

/**
 * 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 = head;
			ListNode *fast = head;
			do
			{
				if(!slow||!fast)
					return nullptr;
				slow = slow->next;
				fast = fast->next;
				if(fast)
					fast = fast->next;
				else
					return nullptr;
			}
			while(slow!=fast);

			slow = head;
			while(slow!=fast)
			{
				slow = slow->next;
				fast = fast->next;
			}
			return slow;
		}
};



相關推薦

[LeedCode OJ]#142 Linked List Cycle II

【 宣告:版權所有,轉載請標明出處,請勿用於商業用途。  聯絡信箱:[email protected]】 題意: 對於一個連結串列,判斷其是否有環,有環則返回環的起始位置。 思路:

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

[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

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

【演算法分析】如何理解快慢指標?判斷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(python)

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note

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. 《程式設計師面試金典》原題 /** * Definition f

【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

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

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

LeetCode-142-Linked List Cycle II

nts note 兩個 color lse bool HERE res listnode 算法描述: Given a linked list, return the node where the cycle begins. If there is no cycle, ret

LeetCode 141, 142. Linked List Cycle I+II

有環 TP clas 起點 urn 沒有 nbsp 快慢指針 list 判斷鏈表有沒有環,用Floyd Cycle Detection算法,用兩個快慢指針。 class Solution { public: bool hasCycle(ListNode *