Linked List Cycle II--寻找单链表中环的起始点
题目要求
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.
Follow up:
Can you solve it without using extra space?
如何找到环的第一个节点?
根据题目意图,我们可以构建如下模型:

设:链表头是X,环的第一个节点是Y,slow和fast第一次的交点是Z。各段的长度分别是a,b,c,如图所示。环的长度是L。slow和fast的速度分别是v,2v。
由slow和fast第一次在点Z相遇,我们可以得出以下等式:
2(a+b)=(a+2b+c)
=> a=c
由此可见,a和c的长度一样。因此我们可以将slow重新定位到头结点,然后fast与slow以相同的速度前进,相遇的节点Y则是环的开始节点。
代码实现:
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode* slow=head;
ListNode* fast=head;
while(true){
if(fast==nullptr||fast->next==nullptr){
return nullptr;
}
slow=slow->next;
fast=fast->next->next;
if(fast==slow){
break;
}
}
slow=head;
while(slow!=fast){
slow=slow->next;
fast=fast->next;
}
return slow;
}
};
Linked List Cycle II--寻找单链表中环的起始点的更多相关文章
- 力扣 ——Linked List Cycle II(环形链表 II) python实现
题目描述: 中文: 给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). ...
- 【Leetcode】【Medium】Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. 解题: ...
- [算法][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 ...
- LeetCode 142. 环形链表 II(Linked List Cycle II)
142. 环形链表 II 142. Linked List Cycle II 题目描述 给定一个链表,返回链表开始入环的第一个节点.如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整 ...
- 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 ...
- 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现
引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...
- 142. Linked List Cycle II【easy】
142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If ther ...
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
随机推荐
- mongodb 定时备份
通过centos 脚步来执行备份操作,使用crontab实现定时功能,并删除指定天数前的备份 具体操作: 1.创建Mongodb数据库备份目录 mkdir -p /home/backup/mongod ...
- Ubuntu Desktop 16.04 LTS 下成功配置Jupyter的两个python内核版本(2.7x,3.5x)
Ubuntu Desktop 16.04 LTS 安装好系统默认就有python两个不同版本(2.7.12和3.5.2) 现在来熟悉一下jupyter的对python这两个不同python版本的内核 ...
- ASP.NET MVC5 Forms登陆+权限控制(控制到Action)
一.Forms认证流程 请先参考如下网址: http://www.cnblogs.com/fish-li/archive/2012/04/15/2450571.html 本文主要介绍使用自定义的身份认 ...
- IDE-Android Studio -FAQ-使用习惯(不断更新 欢迎留言)
摘要: 从ecplise工具切换到android studio后遇到了很多问题,起初亦非常痛苦,城墙内外阅博无数才得以解决.所以把当时遇到的问题记录下来,方便后来人学习. 另如果有遇到未纪录的问题欢迎 ...
- CentOS7从U盘中拷贝文件
1. 要想从U盘中拷贝文件,必须要将U盘挂载到一个目录中,所以必须新建一个目录,一般建在/mnt下.我们执行:mkdir /mnt/usb来新建一个目录. 2. 查看U盘是否已经被识别.执行:df - ...
- 查看centos版本及32还是64位
1.[root@mini1 ~]# cat /etc/issue 2.[root@mini1 ~]# cat /etc/redhat-release 查看位数: [root@mini1 ~]# g ...
- python--socket/Socketerver并发/udp
Socketerve并发 基于tcp套接字,关键就是两个循环,一个链接循环,一个通讯循环 Socketserver模块中分两个大类:server类(解决链接问题)和request类(解决通信问题) s ...
- C语言的一些输出格式
%e printf()的一种输出格式 科学表示的一种浮点数 1.24==1.240000e+000 1240000==1.240000e+006 ...
- 一张图片快速明白Python概述
- JavaScript数据结构与算法(二) 队列的实现
TypeScript方式源码 class Queue { items = []; public enqueue(element) { this.items.push(element); } publi ...