当前位置:网站首页>Linked list: the entry node of the link in the linked list
Linked list: the entry node of the link in the linked list
2022-06-13 02:44:00 【Zeng Qiang】
List of articles
Their thinking
According to the meaning , First of all, we have to judge whether there is a ring in the linked list .
secondly , According to the characteristics of the ring , We define Fast slow double pointer , Move forward , There must be a time to meet , meet The node of is exactly the entry node of the link in the linked list .
A two-step :
- Find the number of nodes in the ring .
- Define double pointer , The previous pointer takes two steps first , The back pointer takes one step , Both pointers continue to move forward at the same time , If the list has links , Then the two pointers must meet , Return to one Nodes in the ring k
- According to the properties of the ring , Along nodes k Move forward , Because there are rings , So the pointer will go back to k node , This process of exploration leads to Number of nodes in the ring N.
- Find the node where the two pointers meet in the ring
- Define fast and slow double pointers ,
- Let's go first N Step .
- Slow pointer , Quick pointer , Moving forward at the same time . When two pointers are equal , Then meet , Return to the encounter node .
Code
/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode inLoop = getNodeInLoop(head);
if(inLoop == null) {
return null; // Determine if the list has links .
}
int loopCount = 1;
for(ListNode node = inLoop; node.next != inLoop; node = node.next){
loopCount++; // Count the number of nodes in the ring .
}
ListNode fast = head;
ListNode slow = head;
for(int i = loopCount; i > 0; i--) {
fast = fast.next;
}
while(slow != fast) {
slow = slow.next;
fast = fast.next;
}
return slow;
}
private ListNode getNodeInLoop(ListNode head) {
if(head == null || head.next ==null) {
return null;
}
ListNode slow = head.next;
ListNode fast = slow.next;
while (slow != null && fast != null) {
if(slow == fast) {
return slow;
}
slow = slow.next;
fast = fast.next;
if(fast != null) {
fast = fast.next;
}
}
return null;
}
}
summary
This topic examines the characteristics of linked lists , That is, how to find whether there are links in the linked list through the fast and slow pointers , Returns the entry node of the ring .
边栏推荐
- redis
- Detailed explanation of data processing in machine learning (I) -- missing value processing (complete code attached)
- CDN single page reference of indexbar index column in vant framework cannot be displayed normally
- Svg filter effect use
- too old resource version,Code:410
- OpenCVSharpSample04WinForms
- Opencvshare4 and vs2019 configuration
- 01 初识微信小程序
- [reading papers] dcgan, the combination of generating countermeasure network and deep convolution
- Data warehouse notes | 5 factors that need attention for customer dimension modeling
猜你喜欢

Hstack, vstack and dstack in numpy

Logiciel professionnel de gestion de base de données: Valentina Studio Pro pour Mac

Prometheus node_exporter安装并注册为服务

专业的数据库管理软件:Valentina Studio Pro for Mac
![Leetcode 450. Delete node in binary search tree [binary search tree]](/img/39/d5c4d424a160635791c4645d6f2e10.png)
Leetcode 450. Delete node in binary search tree [binary search tree]

Data warehouse notes | 5 factors that need attention for customer dimension modeling

Introduction to facial expression recognition system -- offline environment configuration

專業的數據庫管理軟件:Valentina Studio Pro for Mac
![[reading papers] comparison of deeplobv1-v3 series, brief review](/img/80/714b8e5b2ad31b0a1a0b8320a3c714.jpg)
[reading papers] comparison of deeplobv1-v3 series, brief review
![[reading papers] transformer miscellaneous notes, especially miscellaneous](/img/c3/7788b1bcd71b90c18cf66bb915db32.jpg)
[reading papers] transformer miscellaneous notes, especially miscellaneous
随机推荐
Stm32f4 DMA Da sine wave generator keil5 Hal library cubemx
too old resource version,Code:410
Vant框架中关于IndexBar索引栏的CDN单页面引用,无法正常展示
[data and Analysis Visualization] data operation in D3 tutorial 3-d3
数仓笔记|针对客户维度建模需要关注的5个因素
Graph theory, tree based concept
Exam23 named windows and simplified paths, grayscale conversion
Find the number of permutations
[data analysis and visualization] key points of data drawing 5- the problem of error line
Rounding in JS
CDN single page reference of indexbar index column in vant framework cannot be displayed normally
Modify the color of El input, textarea and El checkbox when they are disabled
A wechat app for shopping
Data processing in detailed machine learning (II) -- Feature Normalization
智能安全配电装置如何减少电气火灾事故的发生?
[data analysis and visualization] key points of data drawing 4- problems of pie chart
Open source video recolor code
Digital IC Design -- FIFO design
JS deconstruction assignment
Ijkplayer source code - rendering