★ Linked List Cycle II -- LeetCode

证明单链表有环路:

本文所用的算法 能够 形象的比喻就是在操场其中跑步。速度快的会把速度慢的扣圈

能够证明,p2追赶上p1的时候。p1一定还没有走完一遍环路,p2也不会跨越p1多圈才追上

我们能够从p2和p1的位置差距来证明。p2一定会赶上p1可是不会跳过p1的

由于p2每次走2步。而p1走一步。所以他们之间的差距是一步一步的缩小,4,3,2,1,0

到0的时候就重合了。

找到环路的起点:

既然可以推断出是否是有环路,那改怎样找到这个环路的入口呢?

解法例如以下: 当p2依照每次2步,p1每次一步的方式走,发现p2和p1重合,确定了单向链表有

环路了。

接下来。让p2回到链表的头部。又一次走,每次步长不是走2了,而是走1。那么当p1和p2再次

相遇的时候。就是环路的入口了。

这点能够证明的:

在p2和p1第一次相遇的时候,假定p1走了n步骤,环路的入口是在p步的时候经过的,那么有

1、p1走的路径: p+c = n;        c为p1和p2相交点,距离环路入口的距离。

2、p2走的路径: p+c+k*L = 2*n。  L为环路的周长,k是整数;

将1式中的p+c=n代入到2式,整理得:n=k*L;

所以,假设从p+c点開始,p1再走n步骤的话,还能够回到p+c这个点

同一时候p2从头開始走的话。经过n不,也会达到p+c这点

显然在这个步骤其中p1和p2仅仅有前p步骤走的路径不同,所以当p1和p2再次重合的时候。必

然是在链表的环路入口点上。

code

//Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
#include<iostream>
#include<fstream>
using namespace std;

struct ListNode {
	int val;
	ListNode *next;
	ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
public:
	ListNode *detectCycle(ListNode *head) {
		ListNode* fast_walker = head;
		if (has_cycle(head, fast_walker)){
			ListNode* cur = head;
			while (fast_walker != cur){
				fast_walker = fast_walker->next;
				cur = cur->next;
			}
			return cur;
		}
		else return NULL;
	}
private:
	bool has_cycle(ListNode* head , ListNode* fast_walker){
		ListNode* slow_walker = head;
		while (slow_walker && fast_walker){
			fast_walker = fast_walker->next;
			if (fast_walker) fast_walker = fast_walker->next;
			else break;
			slow_walker = slow_walker->next;
			if (fast_walker == slow_walker) return true;
		}
		return false;
	}
};

int main(){
	fstream fin("test.txt");
	ListNode* head(0);//此时并没有分配存储地址
	ListNode* tmp = head;//此时相当于 tmp = NULL
	int in = 0;
	while (fin >> in){
		if (!head) {
			head = new ListNode(in);
			tmp = head;
		}
		else{
			while (tmp->next != NULL) tmp = tmp->next;
			tmp->next = new ListNode(in);
			tmp = tmp->next;
			tmp->next = NULL;
		}
	}
	//制造一个环
	tmp->next = head->next;
	Solution ss;
	ListNode* retult = ss.detectCycle(head);
	system("pause");
	return 0;
}
时间: 2024-10-26 18:43:00

★ Linked List Cycle II -- LeetCode的相关文章

Linked List Cycle II || LeetCode

/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode *detectCycle(struct ListNode *head) { struct ListNode *fast,*slow; bool flag; fast=slow=head,flag=false; while(fast&&slow){ if

LeetCode 142 链表 Linked List Cycle II

LeetCode 142 链表 Linked List Cycle II LeetCode Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexe

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