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

边栏推荐
- 【社区明星评选】第23期 7月更文计划 | 点滴创作,汇聚成塔!华为FreeBuds 4E等酷爽好礼送不停
- 【合集- 行业解决方案】如何搭建高性能的数据加速与数据编排平台
- Four tips tell you how to use SMS to promote business sales?
- Teach you to quickly set up a live studio in 30 minutes
- EasyNVR平台设备通道均在线,操作出现“网络请求失败”是什么原因?
- The easynvr platform equipment channels are all online. What is the reason for the "network request failure" in the operation?
- Troubleshooting MySQL for update deadlock
- 国内离线安装 Chrome 扩展程序的方法总结
- MySQL事务并发问题和MVCC机制
- 电子元器件招标采购商城:优化传统采购业务,提速企业数字化升级
猜你喜欢

Teach you to quickly set up a live studio in 30 minutes

Countdowncatch and completabilefuture and cyclicbarrier

What if icloud photos cannot be uploaded or synchronized?

PHP uses queues to solve maze problems

Reading notes of "high EQ means being able to talk"

TCP粘包问题

剑指 Offer 16. 数值的整数次方

金融服务行业SaaS项目管理系统解决方案,助力企业挖掘更广阔的增长服务空间

华兴证券:混合云原生架构下的 Kitex 实践

Merged binary tree of leetcode
随机推荐
GameFi链游系统开发NFT技术
一点比较有意思的模块
教你30分钟快速搭建直播间
【TiDB】TiCDC canal_ Practical application of JSON
ForkJoinPool
Infineon - GTM architecture -generic timer module
LeetCode动态规划经典题(一)
Geoffrey Hinton:我的五十年深度学习生涯与研究心法
C WinForm program interface optimization example
Dlib库实现人脸关键点检测(Opencv实现)
传统微服务框架如何无缝过渡到服务网格 ASM
hdfs上的数据导入到clickhouse用什么方式最快呢?spark通过jdbc导入,还是hdfs
What if the apple watch fails to power on? Apple watch can not boot solution!
Another CVPR 2022 paper was accused of plagiarism, and Ping An insurance researchers sued IBM Zurich team
AI chief architect 10-aica-lanxiang, propeller frame design and core technology
Small program container technology to promote the operation efficiency of the park
Courage to be hated: Adler's philosophy class: the father of self inspiration
【TiDB】TiCDC canal_json的实际应用
Tensorflow2 ten must know for deep learning
华兴证券:混合云原生架构下的 Kitex 实践
