当前位置:网站首页>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
边栏推荐
- Awvs batch operation script
- 牛客 BM83 字符串變形(大小寫轉換,字符串反轉,字符串替換)
- Pytorch深度学习和目标检测实战笔记
- C language to realize mine sweeping
- Zzuli:1058 solving inequalities
- To improve efficiency or increase costs, how should developers understand pair programming?
- Zzuli:1049 square sum and cubic sum
- C language dup2 function
- 分布式事务(Seata) 四大模式详解
- Solve the problem that PR cannot be installed on win10 system. Pr2021 version -premiere Pro 2021 official Chinese version installation tutorial
猜你喜欢
Vs+qt application development, set software icon icon
On MEM series functions of C language
[ue4] Niagara's indirect draw
How can entrepreneurial teams implement agile testing to improve quality and efficiency? Voice network developer entrepreneurship lecture Vol.03
Introduction to opengl4.0 tutorial computing shaders
Creation of data table of Doris' learning notes
Analysis of gene family characteristics - chromosome location analysis
Puzzle (016.4) domino effect
Vs+qt multithreading implementation -- run and movetothread
Adobe Premiere Pro 15.4 has been released. It natively supports Apple M1 and adds the function of speech to text
随机推荐
dllexport和dllimport
Byte practice surface longitude
Adc128s022 ADC Verilog design and Implementation
cpu飙升排查方法
China PETG market forecast and Strategic Research Report (2022 Edition)
.NET六大设计原则个人白话理解,有误请大神指正
adc128s022 ADC verilog设计实现
提高效率 Or 增加成本,开发人员应如何理解结对编程?
Output student grades
Zzuli:1045 numerical statistics
My QT learning path -- how qdatetimeedit is empty
牛客 BM83 字符串变形(大小写转换,字符串反转,字符串替换)
556. The next larger element III: simple construction simulation questions
Zzuli:1046 product of odd numbers
Zzuli:1058 solving inequalities
ASTC texture compression (adaptive scalable texture compression)
Zzuli:1044 failure rate
Use of form text box (I) select text
Zzuli:1055 rabbit reproduction
Tensor 省略号(三个点)切片