当前位置:网站首页>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
边栏推荐
- [graphics] hair simulation in tressfx
- C # realizes the login interface, and the password asterisk is displayed (hide the input password)
- 链表有环,快慢指针走3步可以吗
- C language to implement a password manager (under update)
- [engine development] rendering architecture and advanced graphics programming
- PHP GD image upload bypass
- Piwigo 2.7.1 sqli learning
- [ue4] Niagara's indirect draw
- China PETG market forecast and Strategic Research Report (2022 Edition)
- mmdetection 学习率与batch_size关系
猜你喜欢
ASTC texture compression (adaptive scalable texture compression)
Frequently asked questions: PHP LDAP_ add(): Add: Undefined attribute type in
亚马逊、速卖通、Lazada、Shopee、eBay、wish、沃尔玛、阿里国际、美客多等跨境电商平台,测评自养号该如何利用产品上新期抓住流量?
4-24--4-28
Detailed explanation of four modes of distributed transaction (Seata)
Puzzle (016.4) domino effect
Paper sharing: generating playful palettes from images
Open under vs2019 UI file QT designer flash back problem
5-1 blocking / non blocking, synchronous / asynchronous
How to query the baby category of tmall on Taobao
随机推荐
My QT learning path -- how qdatetimeedit is empty
cpu飙升排查方法
Qt development - scrolling digital selector commonly used in embedded system
4-33--4-35
Tonybot Humanoïde Robot Infrared Remote play 0630
Plane vector addition
B2020 分糖果
Vs+qt application development, set software icon icon
Joomla! CMS 3.0~3.4.6 RCE
Solve the problem that PR cannot be installed on win10 system. Pr2021 version -premiere Pro 2021 official Chinese version installation tutorial
4-20-4-23 concurrent server, TCP state transition;
复合类型(自定义类型)
Zhejiang University Edition "C language programming (4th Edition)" topic set reference ideas set
亚马逊、速卖通、Lazada、Shopee、eBay、wish、沃尔玛、阿里国际、美客多等跨境电商平台,测评自养号该如何利用产品上新期抓住流量?
Introduction to opengl4.0 tutorial computing shaders
Output student grades
[combinatorics] permutation and combination (set combination, one-to-one correspondence model analysis example)
406. 根据身高重建队列
How to color ordinary landscape photos, PS tutorial
Zzuli:1044 failure rate