当前位置:网站首页>Double pointer in linked list -- fast and slow pointer
Double pointer in linked list -- fast and slow pointer
2022-07-22 21:14:00 【yizhi_ hao】
Introduce
The single linked list is defined as follows :
class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
Given a single chain table , How to quickly find the midpoint of a single linked list ?
In the normal way , You need to traverse the single linked list twice , First traverse the linked list to get the length of the linked list , Then take half according to the length of the linked list and traverse the single linked list again to get the midpoint of the single linked list .
Usable fast (fast)、 slow (slow) Two pointers traverse the single linked list to complete , Quick pointer fast Two steps at a time , Slow pointer one step at a time , Start traversing the linked list , The cycle condition is while(fast != null && fast.next != null)
- If the length of the single linked list is odd , Then at the end of the cycle ,slow The pointer just points to the midpoint of the linked list
- If the length of the single linked list is even , Then at the end of the cycle ,slow The pointer points to the right node of the middle two nodes
The implementation is as follows :
ListNode getMidNode(ListNode head) {
// Get the midpoint of the linked list
ListNode fast = head;
ListNode slow = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
An application
LeetCode141 Circular list
analysis : Set speed pointer , The fast pointer moves multiple nodes at a time , Slow pointer moves one node at a time , If there is a ring , Fast pointer will “ Catch up ” The node indicated by the slow pointer .
class Solution {
public boolean hasCycle(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while(fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if(fast == slow) {
return true;
}
}
return false;
}
}
边栏推荐
- Human stars website collection plan -- Michael kerrisk
- [lttng learning journey] - before starting
- 使用vis-network根据节点坐标定位环形工具栏
- 嵌入式系统学习笔记
- [lttng practice] - design a set of things to monitor the execution time and cycle of user programs running in a certain cycle -- demand analysis and scheme design
- JUC-7.0-线程协作-CountDownLatch
- 面试突击67:说一下 TCP/IP 协议?以及每层的作用?
- Use vis network to locate the circular toolbar according to the node coordinates
- pkg-config 查找库和用于编译
- [lttng learning journey] - a preliminary study of trace view
猜你喜欢
随机推荐
环境变量配置文件
Pytorch custom data set loading (label in CSV file)
Juc-7.2-thread collaboration condition
[GXYCTF2019]BabyUpload1
Bash基本功能—多命令顺序执行与管道符
[lttng learning journey] - lttng features
(六)vulhub专栏:Apereo-cas 4.x反序列化漏洞
BUUCTF闯关日记--[网鼎杯 2020 青龙组]AreUSerialz
Write a 3D banner using JS
3. Remote connection and SSH
[lttng learning journey] - simply add a trace point to the user program
BUUCTF闯关日记--[CISCN2019 华北赛区 Day2 Web1]Hack World
Buuctf breakthrough diary -- [netding cup 2020 Qinglong group]areuserialz
PKG config lookup library and for compilation
毕设路线—pytorch环境下的深度学习的高光谱图像分类问题
[LTTng学习之旅]------环境搭建
6.管理服务器和服务
Multithread 07 -- ThreadLocal
[LTTng学习之旅]------在用户程序中简单的添加一个trace点
[lttng learning journey] ----- components of lttng deconstruction