LeetCode OJ:Linked List Cycle II(循环链表II)
Given a linked list, return the node where the cycle begins. If there is no cycle, return null
.
Note: Do not modify the linked list.
Follow up:
Can you solve it without using extra space?
和前面那题类似,只不过这里要找到的是环开始的节点,看下面这张图(图是盗的):
当fast以及slow指针在z处相遇的时候,可以得到一个计算式,关于两个指针走过的距离,有fast指针走过的距离是slow指针的2倍,那么有:
a + b + c + b(fast) = 2 * (a + b) => a = c;
那么从链表头在选一个指针,和slow指针一起走,正好可以在环的起始点处相遇,代码如下所示:
/**
* 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) {
if(!head) return NULL;
ListNode * slow = head;
ListNode * fast = head;
while(slow && fast){
slow = slow->next;
fast = fast->next;
if(fast)
fast = fast->next;
if(fast && slow &&fast == slow)
break;
}
if(fast == NULL) return NULL;
ListNode * start = head;
while(slow != start){
slow = slow->next;
start = start->next;
}
return slow;
}
};
还有一篇博客对这个问题还有类似的衍生问题描述的比较清楚,mark一下
LeetCode OJ:Linked List Cycle II(循环链表II)的更多相关文章
- [LeetCode OJ] Linked List Cycle II—Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...
- [LeetCode]141. Linked List Cycle判断循环链表
快慢指针用来判断循环链表 记住 快慢指针有四种常用的应用场景: 1.找到有序链表的中点,快指针到头的时候,慢指针就是中点. 2.判断是不是循环链表,快慢指针相遇就是 3.找到循环链表的起点,以链表头 ...
- 【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] 142. Linked List Cycle II 链表中的环 II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [LeetCode] 141. Linked List Cycle 链表中的环
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...
- 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 ...
- 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现
引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...
- [Leetcode Week6]Linked List Cycle II
Linked List Cycle II 题解 题目来源:https://leetcode.com/problems/linked-list-cycle-ii/description/ Descrip ...
- leetcode 141. Linked List Cycle 、 142. Linked List Cycle II
判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n ...
- [LeetCode] 142. Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...
随机推荐
- 003-spring结合java类调用quartz
一.利弊 针对001 中设置,不方便程序中动态添加任务,只能使用配置进行配置任务, 适用于已知固定时刻需要执行的任务. 针对002中设置,不方便结合调用spring注入的实体 使用于程序内部新增添的任 ...
- android学习五---OpenCV for android环境搭建
学习android的目的是想在手机上实现计算机视觉的算法.一般算法的研究都是在Matlab上进行,但是手机平台没有那么多的计算资源,用matlab显然是不太现实的.而OpenCV是基于C++语言编写的 ...
- linux基础命令(2)
1 nohup命令 如果你正在运行一个进程,而且你想在退出帐户/关闭终端之后继续运行相应的进程,可以使用这个命令,nohup就是不挂起的意思no hang up. 用法: nohup command ...
- java多线程总结(一)
在java中要想实现多线程,有两种手段,一种是继续Thread类,另外一种是实现Runable接口. 对于直接继承Thread的类来说,代码大致框架是: 1 2 3 4 5 6 7 8 9 10 11 ...
- [转]c# 画图中bitmap类处理出图片时,存储的注意事项
今天查找以前写的画图程序,想完善一下,发现 图片添加文字水印时候会有些模糊,特别是小字体的时候特别模糊, 经过一番调适,终于发现了问题 帖上代码,警示自己 System.Drawing.Image i ...
- Java全局变量
全局变量:Java程序中,不能在所有类之外定义全局变量,只能通过在一个类中定义公用.静态的变量来实现一个全局变量.例如:ClassGlobalVar{public static global_var; ...
- Django:学习笔记(3)——REST实现
Django:学习笔记(3)——REST实现 了解REST风格 按照传统的开发方式,我们在实现CURD操作时,会写多个映射路径,比如对一本书的操作,我们会写多个URL,可能如下 web/deleteB ...
- Java技术学习路线
转载 作者:David 链接:https://www.zhihu.com/question/25255189/answer/86898400来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商 ...
- cdojR - Japan
地址:http://acm.uestc.edu.cn/#/contest/show/95 题目: R - Japan Time Limit: 3000/1000MS (Java/Others) ...
- SQL 函数以及SQL 编程
1.数学函数:操作一个数据,返回一个结果 --去上限: ceiling ☆select --去下限:floor ☆select floor(price) from car --ABS 绝对值 --PI ...