Q3: 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?
解决原理
以不同的速度去遍历链表,slow指针速度是每步一个结点,fast指针速度是每步两个结点
slow遍历节点数是m,fast遍历节点数是2m
假设链表带环,则slow指针与fast指针一定相遇,因为两指针的相对速度是每步一个节点
假设环的长度是L,则当相遇时,fast遍历的节点数比slow遍历的节点数多NL个,N为正整数
2m = m + NL ——> m = NL,m是环长度的整数倍
相遇时的节点位置与起始点相距m个节点,即相距环长度的整数倍
这样如果令slow指针指向链表起始点,fast指针仍然指向相遇点,并且让slow指针与fast指针以相同的速度遍历链表
则当slow指针指向环的起始点时,因为fast与slow的相对距离是NL,则此时fast必定也指向环的起始位置
所以,当两指针指向同一节点时,此节点即为环的起始点
代码C++
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode *slow = head;
ListNode *fast = head; while(){
if(!slow)
return NULL;
else
slow = slow->next;
if(!fast->next || !fast->next->next)
return NULL;
else
fast = fast->next->next;
if(slow == fast){
slow = head;
while(slow != fast){
slow = slow -> next;
fast = fast -> next;
}
return slow;
}
}
}
};
未改进
Q3: Linked List Cycle II的更多相关文章
- [算法][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 ...
- 15. 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 solve i ...
- 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 ...
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- 【LeetCode练习题】Linked List Cycle II
Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...
- [Linked List]Linked List Cycle,Linked List Cycle II
一.Linked List Cycle Total Accepted: 85115 Total Submissions: 232388 Difficulty: Medium Given a linke ...
- Linked List Cycle && Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note ...
- LeetCode之“链表”:Linked List Cycle && Linked List Cycle II
1.Linked List Cycle 题目链接 题目要求: Given a linked list, determine if it has a cycle in it. Follow up: Ca ...
- 141. Linked List Cycle&142. Linked List Cycle II(剑指Offer-链表中环的入口节点)
题目: 141.Given a linked list, determine if it has a cycle in it. 142.Given a linked list, return the ...
随机推荐
- Java私有构造器
Java私有构造器:使用private关键字声明的构造函数.由于类的构造函数时私有的,所以此类不能被实例化,同时也不能被继承.<Effective Java>第三条:用私有构造器或者枚举强 ...
- 算法----Magic Index
给定一个数组 A,如果 某个下标 i, 满足 A[i] = i, 则 i 称为 Magic Index. 现在假设 A 中的元素是递增有序的.且不重复,找出 Magic Index. 更进一步,当数组 ...
- LeetCode----Copy List with Random Pointer 深度拷贝,浅度拷贝,Lazy拷贝解析
题目:A linked list is given such that each node contains an additional random pointer which could poin ...
- ubuntu命令chmod755
使用方式 : 在终端切换到文件目录 输入 chmod775 hello.py 这样就将hello.py变成了可执行文件 当然作为 python文件 还需要再开头加上 #!/usr/bin/env py ...
- Ubuntu虚机中SVN连接出错,虚机本机可正常CO,CIN,解决方法
Ubuntu虚机中SVN连接出错,虚机本机可正常CO,CIN,外面机器无法正常连接. 解决: 虚机换个IP即可正常连接,原因不明,有可能为公司网管对该IP做了某些限制. PS:VMware中只需将网络 ...
- Android的切图标准
最近总是有人在问我,Android怎么切图啊,怎么适配啊,不只是Android同行,还有很多新手ui设计师. 于是我就写篇文章,根据我们平时的开发经验,简单的介绍一下吧. 如果UI设计师以1920*1 ...
- 用Inno Setup来解决.NetFramework安装问题
用Inno Setup来解决.NetFramework安装问题 2010-03-01 09:59:26 标签:.Net Framework Setup 休闲 Inno 原创作品,允许转载,转载时请务必 ...
- GridView自定义之前后效果对比
- 打开office弹出steup error 的解决办法
找到 C:\Program Files\Common Files\Microsoft Shared\OFFICE12\Office Setup Controller 将这个文件夹删除
- setInterval 函数传参(方法一)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...