证明单链表有环路:

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



能够证明,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. postgresql 的触发器

    今天编写了一个触发器 功能: 有两个表,当一个表的字段有所改动的时候,另一个表跟着改动 CREATE OR REPLACE FUNCTION process_emp_audit() RETURNS T ...

  2. Dalvik虚拟机进程和线程的创建过程分析

    从前面Dalvik虚拟机的运行过程分析一文可以知道,Dalvik虚拟机除了可以执行Java代码之外,还可以执行Native代码,也就是C/C++函数. 这些C/C++函数在执行的过程中,又可以通过本地 ...

  3. jsp表单提交中文乱码的解决

    <%@ page language="Java" contentType="text/html; charset=utf-8 "    pageEncod ...

  4. js烟花特效

    <!DOCTYPE html><html><head><style>body{background:#000;margin:0;}canvas{curs ...

  5. 对Gearman中client,worker,jobserver的理解

    在gearman的官网http://gearman.org/有以下的一段说明 A Gearman powered application consists of three parts: a clie ...

  6. Web 研发模式演变

    前不久徐飞写了一篇很好的文章:Web 应用的组件化开发.本文尝试从历史发展角度,说说各种研发模式的优劣. 一.简单明快的早期时代 可称之为 Web 1.0 时代,非常适合创业型小项目,不分前后端,经常 ...

  7. jQuery的preventDefault()

    1. 含义: preventDefault() 方法阻止元素发生默认的行为(例如,当点击提交按钮时阻止对表单的提交). 2. 语法: event.preventDefault() 3. 例子: 防止链 ...

  8. CPU 定位高

    流程:把线程dump出来,然后分析 1:Threaddump的方法: kill -3 pid     jstack -l pid     jvisualvm中来thread dump 2:找到导致cp ...

  9. Js 表单序列化

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

  10. 客户端(C#)调用CXF搭建的webservice的出现一些问题记录

    最近把XFire框架搭建的一个webservice换成CXF框架.访问webservice的客户端是C#写的.客户端调用webservice,数据能在客户端得到.看起来显然是成功了. 但其中在VS中添 ...