[LeetCode]Linked List Cycle II解法学习
问题描述如下:
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
Follow up:
Can you solve it without using extra space?
从问题来看,如果可以充分利用额外空间的话,这个题目是不难的,然而题目提出了一个要求,能否在不使用任何额外空间的情况下解决这个问题。
通过反复思考,我觉得这题类似于追击问题,可以用一个快速遍历指针和一个慢速遍历指针对这个链表进行遍历,如果存在环,那么这个快速指针肯定会在环中某一处追到这个慢指针。尽管有这样的想法,但是在判断出了是否有环之后,该如何进一步获取环的起点呢?个人能力有限,之前没有做过类似的题目,本着学习的目的,于是我查阅了资料,在LeetCode的讨论区里得到了解答:
设立一个慢指针slow,一个快指针fast,慢指针每次走一步,快指针每次走两步。假设从head到环起点start的距离为x,环的长度为y(即从起点走回起点所需要走的步数)。
我们知道,当存在环的时候,fast和slow必然会在环中某一处相遇,假设相遇点距离start为m,于是slow和fast在m处相遇时,slow走了:x+ky+m,fast走了:x+ty+m。因为fast走的为slow的两倍,于是:

ty可以被y整除,所以x+m+2ky应当也能被y整除,即(x+m) mod y=0。于是,可以推断,当二者在m出相遇时,再绕环走x步,便一定可以到达环的起点。
可得出代码如下:
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
if(!head) return NULL;
ListNode* slow = head;
ListNode* fast = head;
do{
if(!fast) return NULL;
slow = slow->next;
fast = fast->next;
if(fast) fast = fast->next;
else return NULL;
}while( slow != fast );
slow = head;
while( slow != fast ){
slow = slow->next;
fast = fast->next;
}
return slow;
}
};
[LeetCode]Linked List Cycle II解法学习的更多相关文章
- 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 ...
- LeetCode: Linked List Cycle II 解题报告
Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...
- [LeetCode] Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [Leetcode] Linked list cycle ii 判断链表是否有环
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follo ...
- [LeetCode] Linked List Cycle II, Solution
Question : Given a linked list, return the node where the cycle begins. If there is no cycle, return ...
- LeetCode——Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- Leetcode Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [LeetCode] Linked List Cycle II 链表环起始位置
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- LeetCode Linked List Cycle II 单链表环2 (找循环起点)
题意:给一个单链表,若其有环,返回环的开始处指针,若无环返回NULL. 思路: (1)依然用两个指针的追赶来判断是否有环.在确定有环了之后,指针1跑的路程是指针2的一半,而且他们曾经跑过一段重叠的路( ...
随机推荐
- 原生js解决跨浏览器兼容问题
//跨浏览器兼容问题 Util = { //添加类名 add : function(ele,type,hand){ if(ele.addEventListener){ ele.addEventList ...
- PAT-乙级-1006. 换个格式输出整数 (15)
1006. 换个格式输出整数 (15) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 让我们用字母B来表示“百” ...
- case class inheritance
Scala 禁止case class inheritance case class Person(name: String, age: Int) case class FootballPlayer(n ...
- [Firefly引擎][学习笔记三][已完结]所需模块封装
原地址:http://www.9miao.com/question-15-54671.html 学习笔记一传送门学习笔记二传送门 学习笔记三导读: 笔记三主要就是各个模块的封装了,这里贴 ...
- Windows下如何使用BOOST C++库 .
Windows下如何使用BOOST C++库 我采用的是VC8.0和boost_1_35_0.自己重新编译boost当然可以,但是我使用了 http://www.boostpro.com/produc ...
- [数论]ZOJ3593 One Person Game
题意:一个人要从A走到B 只能走a布.b步.(a+b)步,可以往左或右走 问 最少要走几步,不能走到的输出-1 可以列出方程 $ax+by=A-B$ 或者 $ax+(a+b)y=A-B$ 或者 ...
- Qt:无标题栏无边框程序的拖动和改变大小
From: http://blog.csdn.net/kfbyj/article/details/9284923 最近做项目遇到的问题,总结下. 有时候我们觉得系统的标题栏和按钮太丑太呆板,想做自己的 ...
- Unable to resolve target 'android-8'类似错误的解决办法
导入android项目出现:出现Unable to resolve target 'android-8'错误及其他的一些解决办法 - 为梦想而飞 - 博客频道 - CSDN.NEThttp://blo ...
- HASH暴力破解工具-Hashcat
乌云网看到一篇文章讲述hashcat的使用简介(戳这里),对使用字典破解MD5内容 简单在kali上尝试了一下. (1)首先查看了下hashcat的帮助文档,简单截取了其中的部分常用说明. hashc ...
- 解决git Push时请求username和password,而不是ssh-key验证
转载自:https://blog.lowstz.org/posts/2011/11/23/why-git-push-require-username-password-github/ 之前开始用git ...