证明单链表有环路:

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



能够证明,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. [Python笔记]第十六篇:web框架之Tornado

    Tornado是一个基于python的web框架,xxxxx 安装 python -m pip install tornado 第一个Tornado程序 安装完毕我们就可以新建一个app.py文件,放 ...

  2. UML for Design Pattern

    ************************************************************************************* ************** ...

  3. base64的一个应用情景

    AddActivity.xml rushrank.xml 不过AddActivity.xml不也是通过二进制流就传过去了吗?      事实上是可以的,只要不将这些二进制的数据写下来,传播是可以的,只 ...

  4. SSL交互和握手过程

    SSL消息按如下顺序发送:  1.Client Hello  客户发送服务器信息,包括它所支持的密码组.密码组中有密码算法和钥匙大小: 2.Server Hello  服务器选择客户和服务器都支持的密 ...

  5. JAVA抽象类和接口的深入探讨

    Java 语言中,抽象类(abstract class) 和接口(interface) 是抽象思想的两种体现形式.初学者很容易把这两者搞混,所以Java面试中考抽象类和接口的区别的面试题也常有出现的. ...

  6. struts文件上传拦截器中参数的配置(maximumSize,allowedTypes ,allowedExtensions)问题

    <interceptor-ref name="fileUpload"> <param name="allowedTypes">image ...

  7. MySQL创建用户、授权等

    用于MySQL5.6命令行执行成功: create database wp_people; create user wp_people@'localhost' identified by 'yrwb' ...

  8. 0x1L

    整形常量的后缀表示其类型,包括如下后缀,其中U和L的大小写通用 后缀        类型L           long int LL         long long int U          ...

  9. bzoj 2115: [Wc2011] Xor xor高斯消元

    2115: [Wc2011] Xor Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 797  Solved: 375[Submit][Status] ...

  10. ld: symbol dyld_stub_binding_helper not found, normally in crt1.o/dylib1.o/bundle1.o for architecture i386

    就是选择的运行版本太低了,点击项目,project,把iOS DeployMent Target改为比较高的版本就行