当前位置:网站首页>Entry node of link in linked list - linked list topic
Entry node of link in linked list - linked list topic
2022-06-30 18:58:00 【ailigei】


sketch :
The main requirement of the topic is a linked list with links ( There is no need to prove whether there is a ring ), We need to find the specific node of the linked list as the entry node , For example, above 345 Is a ring node ,3 Is the ring inlet , So we just have to find a way to get 3 This node .
First
My main solution to this problem is ( There is no need to know the solution of the number of nodes in the ring ), Algorithm problems must first be conceived in your mind , What are the specific steps . First, let's briefly talk about how to solve this problem by knowing the number of nodes in the ring , This is simpler , This is the solution that many people should first think of .
It is still the conventional double pointer solution , First step , Set speed pointer ,P1 For fast pointer ,P2 For slow pointer , Let them all point to the head node . In the second step, we need to find the number of nodes in the link in the list (N), Then go ahead N Step . The third step , The fast pointer and the slow pointer move forward at the same speed , When the fast pointer and the slow pointer meet , The node that meets is the entry node of the ring .( The following is an example )
Next, we mainly introduce the solution without knowing the number of nodes in the ring . Although the above solution is easy to think of , But the amount of code is actually quite a lot , First, you need to find the number of nodes in the ring, and then find the meeting node in the double pointer . If you analyze it carefully , You will find that it is not necessary to find the number of nodes in the ring . Set the fast pointer to take two steps at a time , Slow pointer one step at a time , If there are links in the list , The fast and slow pointers must meet at a node in the ring , We can assume a special case to analyze the whole process ( The fast pointer only circulates once in the ring and encounters the slow pointer ), Let the distance from the starting point to the entrance of the ring be X, The entrance from the meeting point to the ring is Y, The distance of one circle of the ring is C, So the distance that the slow pointer goes is X+C-Y, The distance traveled by the fast pointer is X+C+C-Y, And because the speed of the fast pointer is twice that of the slow pointer , So you can get 2(X+C-Y)=X+C+C-Y. After simplification , It can be concluded that X=Y, That is, the distance from the starting point to the entrance of the ring must be equal to the distance from the meeting point to the entrance of the ring , With this conclusion, all the problems were solved , After the fast and slow pointers meet , We just need to set another pointer to the starting point , Then let the slow pointer at the meeting point and the starting point pointer move forward at the same speed , When the starting point pointer meets the slow pointer , The meeting point is the entrance of the ring .
Post code
/* public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; } } */
public class Solution {
public ListNode EntryNodeOfLoop(ListNode pHead) {
if(pHead==null||pHead.next==null)// Determine whether the linked list exists and under special circumstances
{
return null;
}
ListNode fast=pHead;// Quick pointer
ListNode slow=pHead;// Slow pointer
while(fast!=null&&fast.next!=null){
fast=fast.next.next;// Go two steps at a time
slow=slow.next;// Slow pointer one step at a time
if(fast==slow){
ListNode slow2=pHead; Start point pointer
while(slow2!=slow){
// Judge whether the start pointer meets the slow pointer , The meeting point is the entry point of the ring
slow2=slow2.next;
slow=slow.next;
}
return slow2;
}
}
return null;
}
}

边栏推荐
- Flink series: checkpoint tuning
- 【社区明星评选】第23期 7月更文计划 | 点滴创作,汇聚成塔!华为FreeBuds 4E等酷爽好礼送不停
- Rust 书籍资料 - 芽之家书馆
- 系统集成项目管理工程师认证高频考点:编制项目范围管理计划
- Swin-Transformer(2021-08)
- mysql for update 死锁问题排查
- 屏幕显示技术进化史
- In distributed scenarios, do you know how to generate unique IDs?
- Is it safe to open a mobile stock account? Is it reliable?
- [Collection - industry solutions] how to build a high-performance data acceleration and data editing platform
猜你喜欢

程序员女友给我做了一个疲劳驾驶检测

MySQL transaction concurrency and mvcc mechanism

如何利用AI技术优化独立站客服系统?听听专家怎么说!

Redis - persistent RDB and persistent AOF

Detailed single case mode

Vulnerability recurrence ----- 35. Uwsgi PHP directory traversal vulnerability (cve-2018-7490)

php利用队列解决迷宫问题

Deep learning compiler understanding

PyTorch学习(三)

Countdowncatch and completabilefuture and cyclicbarrier
随机推荐
When selecting smart speakers, do you prefer "smart" or "sound quality"? This article gives you the answer
Summary of methods for offline installation of chrome extensions in China
Tensorflow2 ten must know for deep learning
The online procurement system of the electronic components industry accurately matches the procurement demand and leverages the digital development of the electronic industry
Electronic components bidding and purchasing Mall: optimize traditional purchasing business and speed up enterprise digital upgrading
Leader: who can use redis expired monitoring to close orders and get out of here!
医疗行业企业供应链系统解决方案:实现医疗数智化供应链协同可视
Another CVPR 2022 paper was accused of plagiarism, and Ping An insurance researchers sued IBM Zurich team
【TiDB】TiCDC canal_ Practical application of JSON
这里数据过滤支持啥样的sql语句
Tide - 基于 async-std 的 Rust-web 框架
Helping the ultimate experience, best practice of volcano engine edge computing
Glacier teacher's book
Where do the guests come from
如何做好软件系统的需求调研,七种武器让你轻松搞定
OneFlow源码解析:算子签名的自动推断
depends工具查看exe和dll依赖关系
Countdowncatch and completabilefuture and cyclicbarrier
Swin-transformer --relative positional Bias
Rhai 脚本引擎的简单应用示例
