当前位置:网站首页>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;
}
}

边栏推荐
- Adhering to the concept of 'home in China', 2022 BMW children's traffic safety training camp was launched
- Swin-transformer --relative positional Bias
- TCP packet sticking problem
- Small program container technology to promote the operation efficiency of the park
- Compare the audio librosa library with the Mel spectrogram in the torchaudio library
- NFT technology for gamefi chain game system development
- C# Winform程序界面优化实例
- Troubleshooting MySQL for update deadlock
- MySQL事务并发问题和MVCC机制
- countdownlatch 和 completableFuture 和 CyclicBarrier
猜你喜欢

【TiDB】TiCDC canal_ Practical application of JSON

链表中环的入口结点-链表专题

Deep learning compiler understanding

Multipass Chinese document - setting graphical interface

Detailed single case mode

煤炭行业数智化供应商管理系统解决方案:数据驱动,供应商智慧平台助力企业降本增效

医院在线问诊小程序源码 互联网医院源码 智慧医院源码

拓维信息使用 Rainbond 的云原生落地实践

Another CVPR 2022 paper was accused of plagiarism, and Ping An insurance researchers sued IBM Zurich team

领导:谁再用 Redis 过期监听实现关闭订单,立马滚蛋!
随机推荐
小程序容器技术,促进园区运营效率提升
挖财账号开户安全吗?是靠谱的吗?
Tide - 基于 async-std 的 Rust-web 框架
Development and construction of NFT mining tour gamefi chain tour system
Swin-Transformer(2021-08)
Courage to be hated: Adler's philosophy class: the father of self inspiration
先写API文档还是先写代码?
torch stack() meshgrid()
Classic problem of leetcode dynamic programming (I)
《被讨厌的勇气:“自我启发之父”阿德勒的哲学课》
云安全日报220630:IBM数据保护平台发现执行任意代码漏洞,需要尽快升级
When selecting smart speakers, do you prefer "smart" or "sound quality"? This article gives you the answer
LeetCode动态规划经典题(一)
Do you write API documents or code first?
使用excel快速生成sql语句
秉持'家在中国'理念 2022 BMW儿童交通安全训练营启动
如何做好软件系统的需求调研,七种武器让你轻松搞定
SaaS project management system solution for the financial service industry helps enterprises tap a broader growth service space
金融服务行业SaaS项目管理系统解决方案,助力企业挖掘更广阔的增长服务空间
Another CVPR 2022 paper was accused of plagiarism, and Ping An insurance researchers sued IBM Zurich team
