[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的一半,而且他们曾经跑过一段重叠的路( ...
随机推荐
- springMVC上传图片
http://blog.csdn.net/cheung1021/article/details/7084673/ http://toutiao.com/a6293854906445021442/ 工程 ...
- IntelliJ IDEA14 安装
一.官网下载 IntelliJ IDEA的官网:https://www.jetbrains.com/idea/ 进入选择Get IntelliJ IDEA Now ,进入下载页:https://www ...
- Matlab中添加搜索目录
一.问题来源 来自于一份大规模hash图像检索代码. 二.问题解析 2.1 添加目录 addpath('./utils/'); 2.2 添加目录及其子目录 addpath(genpath('./uti ...
- Binary search for the first element greater than target
We all know how to search through an array for an element whose value equals the target value, but h ...
- [转载]c# 多线程一个带多个参数的方法
比如我要线程一个private void subPing(int pre,int end) 我在Thread t=之后应该如何写 用匿名委托吧!那么简单为什么要这样写!t = new Thread(d ...
- hdu 3952
因为一个小错 不过 好不爽........ #include <iostream> #include <fstream> using namespace std; struc ...
- 让wordpress投稿作者在后台只看到自己的文章
wordpress支持多作者撰写,让更多的人参与网站内容的创建是个不错的想法,UGC(User-generated content)使网站主题更丰富,不同的内容吸引不同的受众,一个好的网站应该多产生U ...
- 单例模式Java“完美”实现
我们通过单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访问,从而方便对实例个数的控制并节约系统资源.如果希望在系统中某个类的对象只能存在一个,单例模式是最好的解决方案. public cl ...
- C++ DLL 模板 .
C++ DLL 模板 1.使用VS2005创建Win32 DLL项目,选择空项目,然后加入CppDll.h和CppDll.cpp文件. 2.修改CppDll.h和CppDll.cpp文件使之成为需要的 ...
- POJ3034+DP
题意:给定一个N*N的矩阵, 然后在这个矩阵的每个格子在任意时间会出现一个鼹鼠,这个出现 出现鼹鼠的时间是输出信息所确定的. 现在你手里有一把锤子能够去锤这些鼹鼠. 你能 够移动锤子, ...