当前位置:网站首页>Double pointer of linked list (fast and slow pointer, sequential pointer, head and tail pointer)
Double pointer of linked list (fast and slow pointer, sequential pointer, head and tail pointer)
2022-07-05 22:28:00 【Codestars codestars】
141. Linked List Cycle](https://leetcode.cn/problems/linked-list-cycle/)
Given head, the head of a linked list, determine if the linked list has a cycle in it.
There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail’s next pointer is connected to. Note that pos is not passed as a parameter.
Return true if there is a cycle in the linked list. Otherwise, return false.
At this time, the typical linked list ring problem , The way to solve the circular problem of linked lists is to use fast and slow pointers .
Don't misunderstand the fast and slow pointer , The speed pointer is not the front and back pointer , The fast and slow pointer means a person who walks fast , One walks slowly , For example, one step at a time , Two steps at a time .
In this case , If there is a ring , Then take two steps ( In front of ), We will definitely catch up with each other in the ring ( Walk in the back ), I really want to run on the playground , The fast runner will always surpass the slow one again .
So set two speed pointers , We can judge whether it is a ring by seeing whether it will meet .
public boolean hasCycle(ListNode head) {
if(head==null)return false;
ListNode slowNode=head;
ListNode fastNode=head;
while(fastNode!=null&&fastNode.next!=null){
// In fact, this has to be written on the basis of understanding , Because if there is no ring , that fast Take two steps at a time , It is possible to walk a distance null For two steps , It's also possible to walk a distance null For one step , So I while Nei must write like that
slowNode=slowNode.next;
fastNode=fastNode.next.next;
if(slowNode==fastNode)return true;
}
return false;
}
876. Middle of the Linked List
class Solution {
public ListNode middleNode(ListNode head) {
if(head==null)return null;
ListNode slow=head;
ListNode fast=head;
while(fast.next!=null&&fast.next.next!=null){
fast=fast.next.next;
slow=slow.next;
}
if(fast.next==null)return slow;
if(fast.next.next==null)return slow.next;
return head;
// The principle of this question is
Use the speed pointer , Two steps at a time , Slow pointer one step at a time , So the fast pointer travels twice as far as the slow pointer ,
So when the fast pointer traverses to the end of the linked list , The slow pointer points to the intermediate node ( From the solution of Li Kou problem )
}
At the beginning, I combined this problem with The speed pointer above is confused .
This problem should be called sequential pointer, which is more accurate , Let a pointer go first , Stop when certain conditions are met . Then two pointers go together .
Allied And this question
The idea is exactly the same
Another is the enhanced version of the above circular linked list , Not only to find out whether the linked list has rings , Also find out the starting point of the ring .
The idea is to find the place where the fast and slow pointers meet first , Then put a node at the place where they meet and at the head of the chain , Went together , Where the two nodes meet
Is the starting point of the ring . Specific derivation can refer to Code Capriccio solution
The code is as follows :
public class Solution {
public ListNode detectCycle(ListNode head) {
if(head==null)return null;
ListNode result=head;
ListNode slow=head;
ListNode fast=head;
int count=0;
while(fast.next!=null&&fast.next.next!=null){
slow=slow.next;
fast=fast.next.next;
if(slow==fast) break;
}
if(fast.next==null||fast.next.next==null) return null;
while(result!=slow){
result=result.next;
slow=slow.next;
}
return result;
}
}
边栏推荐
- Promql demo service
- Text组件新增内容通过tag_config设置前景色、背景色
- Post-90s tester: "after joining Ali, this time, I decided not to change jobs."
- Distance entre les points et les lignes
- Nacos installation and service registration
- EasyCVR集群部署如何解决项目中的海量视频接入与大并发需求?
- Pl/sql basic syntax
- Three "factions" in the metauniverse
- Form artifact
- Metaverse Ape猿界应邀出席2022·粤港澳大湾区元宇宙和web3.0主题峰会,分享猿界在Web3时代从技术到应用的文明进化历程
猜你喜欢
分布式解决方案之TCC
ESP32 hosted
元宇宙中的三大“派系”
Oracle advanced query
Oracle hint understanding
Practice: fabric user certificate revocation operation process
Alternating merging strings of leetcode simple questions
[error record] groovy function parameter dynamic type error (guess: groovy.lang.missingmethodexception: no signature of method)
Storage optimization of performance tuning methodology
a-tree 树的全部展开和收起
随机推荐
How can Bluetooth in notebook computer be used to connect headphones
boundary IoU 的计算方式
90后测试员:“入职阿里,这一次,我决定不在跳槽了”
What about data leakage? " Watson k'7 moves to eliminate security threats
Analysis of the problem that the cookie value in PHP contains a plus sign (+) and becomes a space
实战:fabric 用户证书吊销操作流程
The code generator has deoptimised the styling of xx/typescript.js as it exceeds the max of 500kb
70. Climbing Stairs. Sol
Unique occurrence times of leetcode simple questions
opencv 判断点在多边形内外
Why does the C# compiler allow an explicit cast between IEnumerable< T> and TAlmostAnything?
Leetcode simple question: check whether each row and column contain all integers
[error record] file search strategy in groovy project (src/main/groovy/script.groovy needs to be used in the main function | groovy script directly uses the relative path of code)
Some tutorials install the database on ubantu so as not to occupy computer memory?
Metaverse Ape获Negentropy Capital种子轮融资350万美元
笔记本电脑蓝牙怎么用来连接耳机
點到直線的距離直線的交點及夾角
BFC block level formatting context
Record several frequently asked questions (202207)
344. Reverse String. Sol