证明单链表有环路:

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



能够证明,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. HIVE:用外连接替代子查询

    由于hive也支持sql,很多人会把hql跟标准sql进行比较,甚至有的时候会直接套用.hive不支持事务也不支持索引,更不支持追加写,但是对于一般的sql都是能够支持的.但是对于一些子查询确实无法支 ...

  2. Android Studio HelloWorld

    开发第一应用 可以开发属于自己的应用,是否有点小激动?好吧!让我们开始,首先点击Start a new Android Studio Project创建工程: 接下来需要输入应用名称(第一个字母要大写 ...

  3. (转)Eclipse 下找不到或无法加载主类的解决办法

    转自:http://my.oschina.net/leejun2005/blog/106789,写的太好了! 有时候 Eclipse 会发神经,好端端的 project 就这么编译不了了,连 Hell ...

  4. python之requests-multipart/from-data

    示例代码:files = {"token":(None,token), "key":(None,key), "file":"hel ...

  5. bzoj 1041: [HAOI2008]圆上的整点 本原勾股數組

    1041: [HAOI2008]圆上的整点 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2027  Solved: 853[Submit][Stat ...

  6. Android 使用HTTP(get和post)方式登陆服务器

    package com.wuyou.submittoserver; import android.os.Bundle; import android.support.v7.app.ActionBarA ...

  7. Qt 窗体的模态与非模态(setWindowFlags(Qt::WindowStaysOnTopHint);比较有用,还有Qt::WA_DeleteOnClose)

    概念 模态对话框(Modal Dialog)与非模态对话框(Modeless Dialog)的概念不是Qt所独有的,在各种不同的平台下都存在.又有叫法是称为模式对话框,无模式对话框等. 1. 模态窗体 ...

  8. 【转】MFC界面更新实现方法

    原文网址:http://www.cnblogs.com/skywatcher/p/3572311.html 1.更新窗口 即采用UpdateWindow()函数立即发送WM_PAINT消息更新整个窗口 ...

  9. codeforces -- 283A

    A. Cows and Sequence time limit per test 3 seconds memory limit per test 256 megabytes input standar ...

  10. 关于 all-delete-orphan

    当关联双方存在父子关系,就可以在 set 处设定 cascade 为 all-delete-orphan 所谓父子关系,即指由父方控制子方的持久化圣明周期,子方对象必须和一个父方对象关联.如果删除父方 ...