证明单链表有环路:

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



能够证明,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;
}

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

  1. Linked List Cycle II || LeetCode

    /** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * } ...

  2. 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. Foll ...

  3. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  4. LeetCode Linked List Cycle II 和I 通用算法和优化算法

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

  5. 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现

    引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...

  6. LeetCode解题报告—— Linked List Cycle II & Reverse Words in a String & Fraction to Recurring Decimal

    1. Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no ...

  7. [Leetcode Week6]Linked List Cycle II

    Linked List Cycle II 题解 题目来源:https://leetcode.com/problems/linked-list-cycle-ii/description/ Descrip ...

  8. LeetCode 142. 环形链表 II(Linked List Cycle II)

    142. 环形链表 II 142. Linked List Cycle II 题目描述 给定一个链表,返回链表开始入环的第一个节点.如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整 ...

  9. [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环

    题目要求 Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you so ...

随机推荐

  1. session与cookie的区别,有哪些不同之处

    session与cookie的区别,根据自己的理解总结如下: (1)cookie是一种客户端的状态管理技术,将状态写在 浏览器端,而session是一种服务器端的状态管理技术,将 状态写在web服务器 ...

  2. Android内的生命周期整理

    1. Android App的生命周期: 2. Application的生命周期: 3. Activity的生命周期: 3.1 Fragment的生命周期: 4. Service的生命周期:5. Br ...

  3. Android中通过typeface设置字体

    Android系统默认支持三种字体,分别为:“sans”, “serif”, “monospace",除此之外还可以使用其他字体文件(*.ttf)方法一:XML中使用android默认字体 ...

  4. 2016022604 - redis命令介绍

    Redis keys命令用于在Redis的管理键. Redis keys命令使用语法如下所示: redis最新版本目前是3.0.7 redis 127.0.0.1:6379> COMMAND K ...

  5. [BZOJ 1004] [HNOI2008] Cards 【Burnside引理 + DP】

    题目链接:BZOJ - 1004 题目分析 首先,几个定义和定理引理: 群:G是一个集合,*是定义在这个集合上的一个运算. 如果满足以下性质,那么(G, *)是一个群. 1)封闭性,对于任意 a, b ...

  6. 自定义元素–为你的HTML代码定义新元素

    注意:这篇文章介绍的 API 尚未完全标准化,并且仍在变动中,在项目中使用这些实验性 API 时请务必谨慎. 引言 现在的 web 严重缺乏表达能力.你只要瞧一眼“现代”的 web 应用,比如 GMa ...

  7. 最牛「CSRF防护」,带你进入大虾们的圈子!

    简单理解 CSRF 什么是 CSRF? CSRF,通常称为跨站请求伪造,英文名 Cross-site request forgery 缩写 CSRF,是一种对网站的恶意攻击.一个跨站请求伪造攻击迫使登 ...

  8. Keil C51程序调试过程

    用Keil C51编写程序时,经常需要调试,如果不是经常用这个软件的话,很容易忘记这些调试步骤,现在举一个例子“验证延迟函数delay()使其延迟的时间为500ms”说明. 源程序写完后,就可以调试了 ...

  9. Response.ContentType 详细列表 <转>

    Response.ContentType 详细列表   不同的ContentType 会影响客户端所看到的效果.默认的ContentType为 text/html 也就是网页格式.代码如: <% ...

  10. Android项目开发全程(二)--Afinal用法简单介绍

    本篇博文接上篇的<Android项目开发全程(一)--创建工程>,主要介绍一下在本项目中用到的一个很重要的框架-Afinal,由于本系列博文重点是项目开发全程,所以在这里就先介绍一下本项目 ...