★ Linked List Cycle II -- LeetCode
证明单链表有环路:
本文所用的算法 能够 形象的比喻就是在操场其中跑步。速度快的会把速度慢的扣圈
能够证明,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 = NULLint 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相关推荐
- Linked List Cycle II - LeetCode
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note ...
- Leetcode 142. Linked List Cycle II
地址:Leetcode 142. linked list Cycle II 问题描述:检测链表是否存在环,是的话返回环入口,否则返回None. 这道题有两个思路,一个是经典的快慢指针的思路,另外一个是 ...
- LeetCode 142. 环形链表 II(Linked List Cycle II)
142. 环形链表 II 142. Linked List Cycle II 题目描述 给定一个链表,返回链表开始入环的第一个节点.如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整 ...
- 【To Do】LeetCode 142. Linked List Cycle II
LeetCode 142. Linked List Cycle II Solution1:我的答案 这道题多次遇到,牢记此解法 这道题要深思一下,快指针和慢指针的速度对比不同,会产生什么不同的结果? ...
- leetcode day5 -- Reorder List Linked List Cycle II
1. Reorder List Given a singly linked list L: L0→L1→-→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln ...
- LeetCode141 Linked List Cycle. LeetCode142 Linked List Cycle II
链表相关题 141. Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can ...
- leetcode: Linked List Cycle II
http://oj.leetcode.com/problems/linked-list-cycle-ii/ Given a linked list, return the node where the ...
- 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 ...
- Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
最新文章
- 打开ADS提示23111@localhost后提示无法与执照伺服机连线,怎么解决?
- Centos 7 docker 拉取镜像慢
- java 线程状态_面试官问:为什么Java线程没有Running状态?我懵了
- HyperLogLog 算法原理及其在 Redis 中的实现
- nmap隐藏自己扫描
- java 对话框打开与保存
- ❤️《大前端—Babel》
- Java面试题-多线程
- virtualbox虚拟机linux共享文件夹,Virtualbox下linux虚拟机共享文件夹挂载
- 08系统装iss_安全信息系统| ISS | 第1部分
- 为什么要架设移动基站
- windows远程桌面登录不允许空密码
- java 气泡_JAVA实现聊天气泡
- 1024程序员狂欢节,来领当当大额优惠券
- linux buffer io error,Linux Buffer I/O error on device dm-4, logical block,dm-4logical
- 手撕设计模式,如何理解依赖倒置原则和好莱坞原则
- 约瑟夫环(循环数组循环链表)
- 设置双坐标轴(twinx twiny)
- k3note Android8,小米8和oppo k3哪款好 小米8和oppo k3区别对比评测
- 微软官网文档人脸识别API分析