当前位置:网站首页>链表有环,快慢指针走3步可以吗
链表有环,快慢指针走3步可以吗
2022-07-03 14:44:00 【莱特昂】
先说结果,可以走三步,但没必要,反而会影响效率。
下面是推导过程,摘自stackflow。
设非环的部分s步,环t步,快指针速度是慢指针的k倍,快慢指针在距离环开头的 j 步处相遇。
那么,相遇时,慢指针走了s+j,快指针走了s + j + m * t,(m是快指针在环中的圈数)
此时,快指针走的距离时慢指针的k倍,则有等式:
k * (s + j) = s + j + m * t
变换一下:
s + j = (m / k-1)t
根据上面的公式可知,等式左边是慢指针走的步数,是等式右边环长度的倍数,慢指针步数s + j是整数,环长度t也是整数,(m / k-1)中,k-1>0,在其他量都确定的情况下,保持(m / k-1)不变的话,分母k-1越小,则分子m也越小,对应快指针在环中走的圈数m也越小,k=2,3,4···,其中k=2时,m最小,即最快相遇,这样效率最高。
环形链表入环点
leetcode142题:https://leetcode.cn/problems/linked-list-cycle-ii/
同样可以推导一下。
快慢指针走得步数:f=2(s+j)
快慢指针相遇时,f=(s+j) +mt(公式含义同最上)
则可以得出s+j=mt,相遇时绕的圈数刚好等于慢指针走的步数。
假设此时再走 l 步到达环的入口,会与走s步的指针相遇,有mt+l=mt+s (环中加mt还是原点)
合并上面两个式子后得 l=s,即再走非环的部分s步即可到入环点。
如图,紫色节点相遇后,假设快指针再绕一圈到这里,走了b+c,而慢指针走到这里,走了a+b,其中b为公共部分,则a=c。
总结:相遇时,慢指针走的步数刚好为快指针在环中绕圈的总步数,距离入环点的步数刚好是非环路径步数。
参考链接:https://stackoverflow.com/questions/5130246/why-increase-pointer-by-two-while-finding-loop-in-linked-list-why-not-3-4-5
边栏推荐
- Table of mathematical constants by q779
- Four data flows and cases of grpc
- 分布式事务(Seata) 四大模式详解
- Puzzle (016.4) domino effect
- Solve the problem that PR cannot be installed on win10 system. Pr2021 version -premiere Pro 2021 official Chinese version installation tutorial
- C language to realize mine sweeping
- Zzuli:1057 prime number determination
- 亚马逊、速卖通、Lazada、Shopee、eBay、wish、沃尔玛、阿里国际、美客多等跨境电商平台,测评自养号该如何利用产品上新期抓住流量?
- Fundamentals of PHP deserialization
- 7-9 one way in, two ways out (25 points)
猜你喜欢

Understand the application scenario and implementation mechanism of differential segment

The latest M1 dedicated Au update Adobe audit CC 2021 Chinese direct installation version has solved the problems of M1 installation without flash back!

Amazon, express, lazada, shopee, eBay, wish, Wal Mart, Alibaba international, meikeduo and other cross-border e-commerce platforms evaluate how Ziyang account can seize traffic by using products in th

puzzle(016.4)多米诺效应

天谋科技 Timecho 完成近亿元人民币天使轮融资,打造工业物联网原生时序数据库

Tonybot humanoid robot checks the port and corresponds to port 0701

556. The next larger element III
![Luogu p5018 [noip2018 popularization group] symmetric binary tree problem solution](/img/89/da1a3a38e02671628f385de0f30369.png)
Luogu p5018 [noip2018 popularization group] symmetric binary tree problem solution

创业团队如何落地敏捷测试,提升质量效能?丨声网开发者创业讲堂 Vol.03

Sword finger offer 28 Symmetric binary tree
随机推荐
Zzuli:1045 numerical statistics
7-10 stack of hats (25 points) (C language solution)
Luogu p5536 [xr-3] core city solution
How to color ordinary landscape photos, PS tutorial
Stop asking yourself if you are suitable for software testing
Zzuli:1048 factorial table
mmdetection 学习率与batch_size关系
Code writing and playing method of tonybot humanoid robot at fixed distance
The latest M1 dedicated Au update Adobe audit CC 2021 Chinese direct installation version has solved the problems of M1 installation without flash back!
Table of mathematical constants by q779
C language fcntl function
【7.3】146. LRU caching mechanism
Zzuli:1041 sum of sequence 2
Zzuli:1044 failure rate
Adc128s022 ADC Verilog design and Implementation
MySQL multi table query subquery
[combinatorics] permutation and combination (set combination, one-to-one correspondence model analysis example)
洛谷P5194 [USACO05DEC]Scales S 题解
【7.3】146. LRU缓存机制
洛谷P4047 [JSOI2010]部落划分 题解