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 ...
随机推荐
- tornado下使用静态文件和文件缓存
静态文件和文件缓存 1.在应用配置 settings 中指定 static_path 选项来提供静态文件服务: 2.在应用配置 settings 中指定 static_url_prefix 选项来 ...
- cas无缝单点登录(原创)
之前一直有一个问题残绕着自己,今天,终于很粗糙的解决了这个问题. 众所周知,按照cas单点登录,默认情况下,在不登录的情况下,打开网站是必须要跳转到登录页面的.那有什么方法可以控制吗,当然有,很简单, ...
- GTID的主从复制的配置
主库配置: (一).修改配置文件:在my.cnf配置文件中开启如下选项. [mysqld] #GTID: gtid_mode=on enforce_gtid_consistency=on server ...
- C# 建立UDP服务器并接收客户端数据
C# 建立UDP服务器并接收客户端数据 2015-02-11 17:20 1218人阅读 评论(0) 收藏 举报 分类: C#开发技术(22) 版权声明:本文为博主原创文章,未经博主允许不得转载. ...
- java栈的实现
可以采用数组与链表两种方法来实现栈. 1.用数组实现栈 import java.util.Arrays; public class MyStack<E>{ private Object[] ...
- [WebException: The underlying connection was closed: The message length limit was exceeded.]解决方法
[WebException: The underlying connection was closed: The message length limit was exceeded.] Syste ...
- [笔记]Python的调试器pudb简易教程
Linux下运行python脚本,pudb是一个不错的调试器. 语法高亮,断点,调用栈,命令行,都有了,如下图. [安装] pip install pudb [使用] pudb xxx.py [快捷键 ...
- vscode 编写c++
参考了 配置文件之前的操作:https://www.cnblogs.com/lianshuiwuyi/p/8094388.html 配置文件具体内容:https://blog.csdn.net/qq5 ...
- Windows安装多个Tomcat服务
1.下载tomcat解压,并复制三份(用压缩版的不要用安装版的) 2.配置环境变量CATALINA_HOME和CATALINA_BASE .改端口 修改文件server.xml,修改3个端口号 < ...
- jQuery时间轴鼠标悬停动画
在线演示 本地下载