[Leetcode] 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?
题意:给定链表,若是有环,则返回环开始的节点,没有则返回NULL
思路:题目分两步走,第一、判断是否有环,第二若是有,找到环的起始点。关于第一点,可以参考之前的博客 Linked list cycle。这里主要分析在有环的情况下的第二步。

说明:在环中的前进方向是顺时针,起点为X,环入口为Y,相遇点在Z;无环X->Y的距离(节点数)为a,相遇点Z到Y的距离为b,环长r。
慢指针一次走一步,即slow=slow->next;快指针一次走两步,即fast=fast->next->next。第一相遇在Z点时,slow走过的距离为a+b,fast走过的距离为a+mr+b (m>=1),解释两个问题:一、为什么能遇到?二、为什么第一次相遇时,慢指针slow没有在环内走过n(0<n<m)圈?针对问题一,因为,快指针fast相对慢指针的的速度是1,即,可以看成,慢指针slow静止不动,而快指针fast以速度为1的情况下,在环内前行,所以能遇到。针对问题二,同样的道理,因为fast相对slow的速度是1,假设slow静止,则在fast以速度1完成一圈之前,肯定能遇到slow。
下面分析相遇时,走过的距离,即结点数。slow:a+b;fast:2*(a+b);所以2*(a+b)=a+mr+b(m>=1),故得到a+b=mr,即a=mr-b;所以讲slow放在X点,fast了依旧在Z点,只不过fast的速度降为1,这样,当X到Y的时候,fast也到Y,即得到环的入口点。
/**
* 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(fast&&fast->next)
{
slow=slow->next;
fast=fast->next->next;
if(slow==fast)
break;
}
//不存在环
if(fast==NULL||fast->next==NULL)
return NULL;
//存在环,slow从头开始,fast从相遇点开始单步走,再次相遇即环的开始点
slow=head;
while(slow !=fast)
{
slow=slow->next;
fast=fast->next;
}
return slow;
}
};
//参考了Cyberspace_TechNode的博客!!!
相关问题的扩展:参考南京大乱炖
1)环的长度是多少?
方法一:从相遇点开始,让慢指针接着走,并计数,当慢指针slow和快指针再次相遇时,返回计数器的值就行
方法二:第一次相遇后,让slow,fast继续走,记录到下次相遇时循环了几次。因为当fast第二次到达Z点时,fast走了一圈,slow走了半圈,而当fast第三次到达Z点时,fast走了两圈,slow走了一圈,正好还在Z点相遇。
2)如何将有环的链表变成单链表(即解除环)?
在本题的基础上,从环的入口开始,当指针的next为环的入口结点时,令next指向NULL即可。
[Leetcode] Linked list cycle ii 判断链表是否有环的更多相关文章
- [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 & 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. Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...
- LeetCode Linked List Cycle II 单链表环2 (找循环起点)
题意:给一个单链表,若其有环,返回环的开始处指针,若无环返回NULL. 思路: (1)依然用两个指针的追赶来判断是否有环.在确定有环了之后,指针1跑的路程是指针2的一半,而且他们曾经跑过一段重叠的路( ...
- 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 ...
- Leetcode142. Linked List Cycle II环形链表2
给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 说明:不允许修改给定的链表. 进阶: 你是否可以不用额外空间解决此题? 方法一:使用map 方法二: 分两个步骤,首先通 ...
- 【LeetCode】Linked List Cycle II(环形链表 II)
这是LeetCode里的第142道题. 题目要求: 给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 说明:不允许修改给定的链表. 进阶:你是否可以不用额外空间解决此题? ...
- leetcode 142. Linked List Cycle II 环形链表 II
一.题目大意 https://leetcode.cn/problems/linked-list-cycle-ii/ 给定一个链表的头节点 head ,返回链表开始入环的第一个节点. 如果链表无环,则 ...
随机推荐
- MySQL数据表操作(DDL)
一.创建数据表 语法:create table 表名称(字段 字段类型 [字段属性],字段 字段类型 [字段属性],...) [表选项]; 表选项:数据表的属性,一般包括engine.charset. ...
- linux的date常用命令
1.显示现在时间 date 2.显示今天日期 date +"%F" date +"%Y-%m-%d" 3.现在时间转化为时间戳 date +%s 4.指定某日期 ...
- Go搭建一个Web服务器
package main import ( "fmt" "net/http" "strings" "log" ) fun ...
- stm32+lwip(五):以太网帧发送测试
我是卓波,很高兴你来看我的博客. 系列文章: stm32+lwip(一):使用STM32CubeMX生成项目 stm32+lwip(二):UDP测试 stm32+lwip(三):TCP测试 stm32 ...
- Django学习之天气调查实例(1):工程的开始
开始学习Django,一步一个脚印的进行.思考再三,还是以一个实例来开始学习.手里面正好有几万条单位天气传感器收集的数据,想做一个网页版的天气统计查询之类的小应用,也可以给学生体验,方便教学的进行(尽 ...
- Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. org/apache/hadoop/hbase/
Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. org/apache/hadoop/hbase/ ...
- 解析HTML利器AngleSharp介绍
解析HTML利器AngleSharp介绍 AngleSharp是基于.NET(C#)开发的专门为解析xHTML源码的DLL组件. 项目地址:https://github.com/FlorianRapp ...
- c++ list_iterator demo
#include <iostream> #include <list> using namespace std; typedef list<int> Integer ...
- 如何激活win10
第一步:用管理员权限打开命令提示符: 第二步:输入命令---slmgr.vbs /upk (成功卸载了产品密钥) 第三步:slmgr /ipk NPP ...
- Clean Code 《代码整洁之道》前四章读书笔记
第一章: 整洁的代码只做好一件事 减少重复代码 提高表达力 提早构建简单抽象 让营地比你来时更干净 第二章:有意义的命名 名副其实:如果名称需要注释来补充,就不算是名副其实. ...