当前位置:网站首页>Double pointeur de liste liée (pointeur rapide et lent, pointeur séquentiel, pointeur de tête et de queue)
Double pointeur de liste liée (pointeur rapide et lent, pointeur séquentiel, pointeur de tête et de queue)
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.
C'est le problème typique de l'anneau de liste , L'approche du problème de l'anneau de liste est un pointeur rapide et lent .
Les pointeurs rapides et lents ne doivent pas être mal compris , Les pointeurs rapides et lents ne sont pas des pointeurs avant et arrière , Les pointeurs rapides et lents signifient une marche rapide , Une marche lente , Comme marcher un pas à la fois ,Deux pas chacun..
Dans ce cas, Si un anneau apparaît , Alors, deux pas (Devant), Je suis sûr qu'un pas dans le ring ( Marche derrière ), Je veux courir sur le terrain de jeu , Celui qui court vite dépassera toujours celui qui court lentement .
Alors réglez deux pointeurs rapides et lents , Voir si vous vous rencontrerez peut déterminer si c'est un anneau .
public boolean hasCycle(ListNode head) {
if(head==null)return false;
ListNode slowNode=head;
ListNode fastNode=head;
while(fastNode!=null&&fastNode.next!=null){
// En fait, ça doit être écrit sur la base de la compréhension , Parce que si c'est sans anneau ,AlorsfastDeux pas à la fois, Il est possible de marcher jusqu'à la distance nullPour deux pas, Ou peut - être à distance null Pour une étape ,C'est tout.while Ça doit être écrit comme ça
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;
// Le principe de cette question est
Avec un pointeur rapide et lent,Le pointeur rapide marche deux pas à la fois,Le pointeur lent fait un pas à la fois, Donc le pointeur rapide marche deux fois plus loin que le pointeur lent ,
Donc quand le pointeur rapide traverse la fin de la liste , Pointeur lent pointant vers le noeud intermédiaire (De la solution de la boucle de force)
}

Au début, j'ai comparé cette question à Le pointeur de vitesse ci - dessus est confus .
Ce problème devrait être appelé pointeur séquentiel plus précis , Laissez un pointeur aller en premier , Arrêtez - vous quand certaines conditions sont remplies . Et puis deux autres pointeurs ensemble .
Similaire Et cette question 
L'idée est exactement la même
L'autre est une version améliorée de la liste de liens en boucle ci - dessus , Il ne suffit pas de trouver si la liste a des anneaux , Et trouver le point de départ de l'anneau .
L'idée est de trouver d'abord où les pointeurs rapides et lents se rencontrent , Ensuite, placez un noeud au point de rencontre et au début de la chaîne ,On y va ensemble., Où les deux noeuds se rencontrent
C'est le point de départ de l'anneau . Pour plus de détails, voir La solution à l'enregistrement aléatoire de code
Les codes sont les suivants::
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;
}
}
边栏推荐
- The countdown to the launch of metaverse ape is hot
- Granularity of blocking of concurrency control
- Go language learning tutorial (XV)
- Distributed resource management and task scheduling framework yarn
- How can Bluetooth in notebook computer be used to connect headphones
- Nanjing: full use of electronic contracts for commercial housing sales
- Platformio create libopencm3 + FreeRTOS project
- Text组件新增内容通过tag_config设置前景色、背景色
- A substring with a length of three and different characters in the leetcode simple question
- What if the files on the USB flash disk cannot be deleted? Win11 unable to delete U disk file solution tutorial
猜你喜欢

The statistics of leetcode simple question is the public string that has appeared once

Stored procedures and stored functions
![[groovy] groovy dynamic language features (automatic type inference of function arguments in groovy | precautions for function dynamic parameters)](/img/ab/086c7455706eecc467eef92c660803.jpg)
[groovy] groovy dynamic language features (automatic type inference of function arguments in groovy | precautions for function dynamic parameters)

Navigation day answer applet: preliminary competition of navigation knowledge competition

How to quickly experience oneos

Qtquick3d real time reflection

Win11 runs CMD to prompt the solution of "the requested operation needs to be promoted"

Alternating merging strings of leetcode simple questions

笔记本电脑蓝牙怎么用来连接耳机

Server optimization of performance tuning methodology
随机推荐
A substring with a length of three and different characters in the leetcode simple question
如何开发引入小程序插件
thinkphp5.1跨域问题解决
GWT module may need to be (RE) compiled reduce - GWT module may need to be (RE) compiled reduce
[groovy] groovy dynamic language features (automatic type inference of function arguments in groovy | precautions for function dynamic parameters)
Oracle is sorted by creation time. If the creation time is empty, the record is placed last
实战:fabric 用户证书吊销操作流程
How to reverse a string fromCharCode? - How to reverse String. fromCharCode?
Understand the basic concept of datastore in Android kotlin and why SharedPreferences should be stopped in Android
Sub total of Pico development
科技云报道:算力网络,还需跨越几道坎?
抖音__ac_signature
Sparse array [matrix]
2022-07-05: given an array, you want to query the maximum value in any range at any time. If it is only established according to the initial array and has not been modified in the future, the RMQ meth
Metaverse Ape获Negentropy Capital种子轮融资350万美元
MySQL服务莫名宕机的解决方案
FBO and RBO disappeared in webgpu
Pl/sql basic case
Oracle advanced query
Draw a red lantern with MATLAB