当前位置:网站首页>链表专项之环形链表
链表专项之环形链表
2022-08-05 07:33:00 【风起、风落】
文章目录
前言
1.141. 环形链表
给你一个链表的头节点 head ,判断链表中是否有环。
如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。注意:pos 不作为参数进行传递 。仅仅是为了标识链表的实际情况。
如果链表中存在环 ,则返回 true 。 否则,返回 false 。
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */
bool hasCycle(struct ListNode *head) {
struct ListNode*fast=head;
struct ListNode*slow=head;
while(fast!=NULL&&fast->next!=NULL)
{
slow=slow->next;
fast=fast->next->next;
if(slow==fast)
{
return true;
}
}
return false;
}
这道题的思考还是比较简单的 使用快慢指针
两者从head开始
快指针每次走2步
慢指针每次走1步
如果是循环链表则 一定能使快指针追上慢指针
也要注意一个问题 判断slow指针与fast指针是否相等 ,是在移动一次之后
因为 开始定义的时候就是相等的
证明为什么快指针一定为2步,慢指针一定为1步
1.当循环链表前的距离与循环链表后的距离相等时
>当slow指针走到循环开始时,fast指针已经走完一圈了,所以两者处于相同位置
2.当循环链表前的距离为循环链表后的距离的1/2
fast一次走2步,slow一次走1步
当slow走到循环开始的位置, fast已经走到循环的一半
按照顺时针的顺序 fast追slow, 两者之间的距离为x
当fast与slow每走一次则之间的距离减少1
即x-1,x-2,x-3,x-4,x-5,x-6…
无论x为奇数或者偶数 两者一定能相遇
同种情况下,fast走N步,slow走1步
依旧假设fast指针与slow指针之间的距离为x
若fast指针一次走3步,slow指针一次走1步
则slow与fast每走一次距离减少2
x-2,x-4,x-6,x-8,x-10…
x若为偶数则能成功遇见 ,若为奇数 就会一直错过 ,造成死循环
同理 若fast指针一次走4步,slow指针一次走1步
两者之间每次减少3
x-3,x-6,x-9,x-12…
若x为奇数则能成功遇见,若为偶数 就会一直错过,造成死循环
142. 环形链表 II
给定一个链表的头节点 head ,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。
如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。如果 pos 是 -1,则在该链表中没有环。注意:pos 不作为参数进行传递,仅仅是为了标识链表的实际情况。
不允许修改 链表。
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */
struct ListNode *detectCycle(struct ListNode *head) {
struct ListNode*fast=head;
struct ListNode*slow=head;
while(fast&&fast->next)
{
fast=fast->next->next;
slow=slow->next;
if(fast==slow)
{
struct ListNode*cur=slow;
while(head!=cur)
{
cur=cur->next;
head=head->next;
}
return cur;
}
}
return NULL;
}
这道题需要一个公式,这个公式需要推导下
它分为 正常情况与特殊情况
公式的推导
1.正常情况
fast走2步,slow走1步
没循环的链表距离为L ,slow在循环链表中走的距离为X,循环链表一圈距离为C
slow走的长度为L+X,fast在循环链表为L+C+X
当fast与slow相遇时 fast是slow走的2倍
即 2(L+X)=L+C+X
L=C-X
相当于head到交点的距离等于相遇点到交点的距离
2.特殊情况
没循环的链表距离为L ,slow在循环链表中走的距离为X,循环链表一圈距离为C
当循环链表C很小时
则当slow刚进入循环时,fast已经转了N圈
当fast与slow相遇时
即 2(L+X)=L+N * C+X
即 L=N*C-X
此时的N * C代表从相遇点处开始的N圈 减去x正好为相遇点
所以N的数量不会影响
依旧从fast与slow的相遇点开始 到交点的距离与head到交点的距离相等
边栏推荐
- 利用Jenkins的持续集成
- 风控特征的优化分箱,看看这样教科书的操作
- [Shanghai] Hiring .Net Senior Software Engineer & BI Data Warehouse Engineer (Urgent)
- 行业应用软件项目经理三步曲
- C语言制作-QQ聊天室
- AI + video technology helps to ensure campus security, how to build a campus intelligent security platform?
- 强网杯2022 pwn 赛题解析——house_of_cat
- 线程池的使用(结合Future/Callable使用)
- Modeling of the MAYA ship
- Use of thread pool (combined with Future/Callable)
猜你喜欢
唤醒手腕 - 微信小程序、QQ小程序、抖音小程序学习笔记(更新中)
Falsely bamboo brother today and found a localization of API to use tools
MySQL: order by sorting query, group by grouping query
400 times performance improvement 丨 swap valuation optimization case calculation
配合屏幕录像专家,又小又清晰!
TRACE32——Break
busybox 知:构建
支持触屏slider轮播插件
二叉树进阶复习1
Task flow scheduling tool AirFlow,, 220804,,
随机推荐
【Dynamic type detection Objective-C】
AI + video technology helps to ensure campus security, how to build a campus intelligent security platform?
在anaconda Promat界面import torch通过,在jupyter notebook中报错的问题(仅提供思路理解!)
MongoDB 语法大全
Liunx教程超详细(完整)
Summary of Text Characterization Methods
Access Denied: "microsoft.web.ui.webcontrols" workaround
Mysql 死锁和死锁的解决方案
RNote108---Display the running progress of the R program
MySQL: join query | inner join, outer join
SVG big fish eat small fish animation js special effects
Flink Learning 10: Use idea to write WordCount and package and run
二叉树进阶复习1
Qt编写自定义控件:文字聚光灯效果之一
餐饮大单品「真香」,却没有穿透周期的能力
[Shanghai] Hiring .Net Senior Software Engineer & BI Data Warehouse Engineer (Urgent)
v-if/v-else根据计算判断是否显示
执子之手,与子偕老。你同意么?
外企Office常用英语
Modeling of the MAYA ship