当前位置:网站首页>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 .
边栏推荐
- vant实现移动端的适配
- Laravel permission export
- [data and Analysis Visualization] data operation in D3 tutorial 3-d3
- Leetcode 450. Delete node in binary search tree [binary search tree]
- Delphi implements adding a column of serial number to the CXGRID list
- C # illustrated tutorial (Fourth Edition) chapter7-7.2 accessing inherited members
- [reading papers] transformer miscellaneous notes, especially miscellaneous
- 02 优化微信开发者工具默认的结构
- Stm32f4 DMA Da sine wave generator keil5 Hal library cubemx
- [reading point paper] deeplobv3+ encoder decoder with Atlas separable revolution
猜你喜欢
Opencv 15 face recognition and eye recognition
[data analysis and visualization] key points of data drawing 6- too many data groups
Special topic I of mathematical physics of the sprint strong foundation program
04路由跳转并携带参数
Ijkplayer source code ---packetqueue
PCR validation of basic biological experiments in [life sciences]
Detailed explanation of data processing in machine learning (I) -- missing value processing (complete code attached)
[reading papers] comparison of deeplobv1-v3 series, brief review
Pycharm installation pyqt5 and its tools (QT designer, pyuic, pyrcc) detailed tutorial
How did you spend your winter vacation perfectly?
随机推荐
[reading papers] comparison of deeplobv1-v3 series, brief review
Redirection setting parameters -redirectattributes
Linear, integer, nonlinear, dynamic programming
Detailed explanation of data processing in machine learning (I) -- missing value processing (complete code attached)
Hstack, vstack and dstack in numpy
[data and Analysis Visualization] D3 introductory tutorial 1-d3 basic knowledge
[reading point paper] yolo9000:better, faster, stronger, (yolov2), integrating various methods to improve the idea of map and wordtree data fusion
Graduation project - campus old thing recycling system based on stm32
[data analysis and visualization] key points of data drawing 4- problems of pie chart
智能安全配电装置如何减少电气火灾事故的发生?
05 tabbar navigation bar function
[reading papers] visual convolution zfnet
Opencvsharp4 handwriting recognition
nn. Conv2d and nn Convtranspose2d differences
Use of OpenCV 11 kmeans clustering
Opencv 08 demonstrates the effect of opening and closing operations of erode, dilate and morphological function morphologyex.
Several articles on norms
[deep learning] fast Reid tutorial
How to select fund products? What kind of fund is a good fund?
[data and Analysis Visualization] D3 introductory tutorial 2- building shapes in D3