当前位置:网站首页>There are links in the linked list. Can you walk three steps faster or slower
There are links in the linked list. Can you walk three steps faster or slower
2022-07-03 14:52:00 【Leton】
Let's start with the results , You can take three steps , But it is not necessary to , It's going to affect efficiency .
Here's the derivation process , Excerpt from stackflow.
Let the non ring part s Step , Ring t Step , A fast pointer is a slow pointer k times , The speed pointer is at the beginning of the distance ring j Meet at step .
that , When we met , Slow down s+j, It's almost gone s + j + m * t,(m Is the number of turns of the fast pointer in the ring )
here , The distance of the fast pointer is slower than that of the slow pointer k times , There is an equation :
k * (s + j) = s + j + m * t
Change it. :
s + j = (m / k-1)t
According to the above formula , On the left side of the equation is the number of steps taken by the slow pointer , Is a multiple of the length of the ring on the right side of the equation , Slow pointer steps s + j Is an integer , Ring length t It's also an integer ,(m / k-1) in ,k-1>0, When other quantities are determined , keep (m / k-1) If it doesn't change , The denominator k-1 The smaller it is , Then molecule m The smaller , Corresponds to the number of turns the fast pointer travels in the ring m The smaller ,k=2,3,4···, among k=2 when ,m Minimum , That is, the fastest meeting , This is the most efficient .
Ring linked list entry point
leetcode142 topic :https://leetcode.cn/problems/linked-list-cycle-ii/
You can also deduce .
How many steps does the fast and slow pointer take :f=2(s+j)
When the fast and slow pointers meet ,f=(s+j) +mt( The meaning of the formula is the same as the top )
Then we can get s+j=mt, The number of turns around when meeting is just equal to the number of steps taken by the slow pointer .
Suppose you leave at this time l Step to the entrance of the ring , Will and go s The pointer of the step meets , Yes mt+l=mt+s ( Ring plus mt Or the origin )
After combining the above two formulas, we get l=s, That is, go to the non ring part s Step to the entry point .
Pictured , After purple nodes meet , Suppose the fast pointer goes around here again , go b+c, And the slow pointer goes here , go a+b, among b For the public part , be a=c.
summary : When we met , The number of steps taken by the slow pointer is exactly the total number of steps taken by the fast pointer in the ring , The number of steps from the entry point is exactly the number of non loop path steps .
Reference link :https://stackoverflow.com/questions/5130246/why-increase-pointer-by-two-while-finding-loop-in-linked-list-why-not-3-4-5
边栏推荐
- Dllexport and dllimport
- 亚马逊、速卖通、Lazada、Shopee、eBay、wish、沃尔玛、阿里国际、美客多等跨境电商平台,测评自养号该如何利用产品上新期抓住流量?
- [engine development] in depth GPU and rendering optimization (basic)
- 牛客 BM83 字符串变形(大小写转换,字符串反转,字符串替换)
- Yolov5进阶之九 目标追踪实例1
- 4-24--4-28
- 光猫超级账号密码、宽带账号密码 获取
- Zzuli:1052 sum of sequence 4
- Detailed explanation of four modes of distributed transaction (Seata)
- Zzuli:1059 highest score
猜你喜欢

C language DUP function

Analysis of gene family characteristics - chromosome location analysis

Dllexport and dllimport

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

Byte practice surface longitude

Use of constraintlayout

远程服务器后台挂起 nohup
![Luogu p4047 [jsoi2010] tribal division solution](/img/7f/3fab3e94abef3da1f5652db35361df.png)
Luogu p4047 [jsoi2010] tribal division solution

4-29——4.32

Showmebug entered Tencent conference, opening the era of professional technical interview
随机推荐
Common shortcut keys in PCB
【7.3】146. LRU caching mechanism
亚马逊、速卖通、Lazada、Shopee、eBay、wish、沃尔玛、阿里国际、美客多等跨境电商平台,测评自养号该如何利用产品上新期抓住流量?
QT - draw something else
Luogu p5194 [usaco05dec]scales s solution
表单文本框的使用(一) 选择文本
零拷贝底层剖析
Qt development - scrolling digital selector commonly used in embedded system
C language fcntl function
How does vs+qt set the software version copyright, obtain the software version and display the version number?
牛客 BM83 字符串变形(大小写转换,字符串反转,字符串替换)
To improve efficiency or increase costs, how should developers understand pair programming?
Zzuli:1056 lucky numbers
NOI OPENJUDGE 1.6(09)
Table of mathematical constants by q779
China PETG market forecast and Strategic Research Report (2022 Edition)
tonybot 人形机器人 红外遥控玩法 0630
创业团队如何落地敏捷测试,提升质量效能?丨声网开发者创业讲堂 Vol.03
Tonybot humanoid robot infrared remote control play 0630
Open under vs2019 UI file QT designer flash back problem