证明单链表有环路:

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



能够证明,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. u-boot子目录Makefile分析

    一.概述 u-boot的子目录Makefile是整个Makefile体系的重要组成部分,决定了对应子目录的编译过程. 二.分析 以cpu/arm920t/Makefile为例进行说明 (1)首先,调用 ...

  2. git+Coding.netの小试牛刀

    一.将本地项目推送到Coding中 1.在Coding中新建项目,填写项目名称和项目描述,设置属性,勾选初始化仓库

  3. word的不同章节之间添加不同的页眉

    1.点击空百处 2. 在页面布局中,找到分隔符,选择”连续“选项,即在空白处插入分隔符 特别注意:这里分隔符会出现换行现象,请选择空白处,不要影响原先布局 3. 当编辑下一个页眉时,点击“链接到前一条 ...

  4. [BZOJ 1733] [Usaco2005 feb] Secret Milking Machine 【二分 + 最大流】

    题目链接:BZOJ - 1733 题目分析 直接二分这个最大边的边权,然后用最大流判断是否可以有 T 的流量. 代码 #include <iostream> #include <cs ...

  5. Js 表单序列化

    http://www.w3cmm.com/javascript/serialize-form.html

  6. windows 版Tomcat 7.0的配置

    http://hi.baidu.com/liguohei/item/d6ea23ff904f98753d198b24 测试成功,截图 ~~~~~~~~~~~~~~~ 一.安装JDK 1.7 1.添加环 ...

  7. tomcat 系统架构与设计模式 第二部分 设计模式 转

    Tomcat 系统架构与设计模式,第 2 部分: 设计模式分析 许 令波, Java 开发工程师, 淘宝网 许令波,现就职于淘宝网,是一名 Java 开发工程师.对大型互联网架构设计颇感兴趣,并对一些 ...

  8. Selenium如何实现dropbar移动

    遇到这个拖拽的dropbar,如何实现呢,, 经过网上查找,可以用Action的方式实现或者js来控制 原理:移动按钮的同时,数字也随着变化 解决方法:1.最简单的就是直接在文本框输入相应的数字 2. ...

  9. Unity3d 获取屏幕depth与normal

    Depth 获取Depth的几种方法,分别有不同效果 1. <span style="font-size:14px;">            float2 depth ...

  10. [转]让程序在崩溃时体面的退出之Unhandled Exception

    原文地址:http://blog.csdn.net/starlee/article/details/6613424 程序是由代码编译出来的,而代码是由人写的.人非圣贤,孰能无过.所以由人写的代码有缺陷 ...