当前位置:网站首页>Leetcode simple question sharing (20)
Leetcode simple question sharing (20)
2022-07-07 13:39:00 【PigeonEssence】
876. The middle node of a list
This is a simple linked list problem , The core problem is to find the central point .
Because this linked list is not a two-way linked list , So we can't simply consider the idea of double pointer reverse traversal . So a fast and slow pointer is a good way .
The concept of fast and slow pointer is that the slow pointer takes one step at a time , Then he goes n The result of this time is n;
Two steps at a time , go n The result of this time is 2n.
Then when the fast pointer goes to the end of the linked list When , That is, the value of the fast pointer is null or the next one of the fast pointer is null . This is the node pointed by the slow pointer, which is the central node .
The code is as follows :
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode middleNode(ListNode head) {
// Slow pointer
ListNode slowPointer = head;
// Quick pointer
ListNode fastPointer = head;
// Jump out of the loop when the fast pointer is empty or the next node of the fast pointer is empty
while(fastPointer!=null && fastPointer.next!=null){
// Slow pointer one step
slowPointer = slowPointer.next;
// Let's go two steps
fastPointer = fastPointer.next.next;
}
// Return slow pointer
return slowPointer;
}
}
边栏推荐
- Build a secure and trusted computing platform based on Kunpeng's native security
- ESP32系列专栏
- Enregistrement de la navigation et de la mise en service du robot ROS intérieur (expérience de sélection du rayon de dilatation)
- Mongodb replication (replica set) summary
- Redis只能做缓存?太out了!
- flask session伪造之hctf admin
- [QNX hypervisor 2.2 user manual]6.3.4 virtual register (guest_shm.h)
- MySQL error 28 and solution
- 记一次 .NET 某新能源系统 线程疯涨 分析
- Pcap learning notes II: pcap4j source code Notes
猜你喜欢
xshell连接服务器把密钥登陆改为密码登陆
实现IP地址归属地显示功能、号码归属地查询
得物客服热线的演进之路
干货|总结那些漏洞工具的联动使用
OSI seven layer model
Scrapy教程经典实战【新概念英语】
Final review notes of single chip microcomputer principle
JS slow motion animation principle teaching (super detail)
Vscade editor esp32 header file wavy line does not jump completely solved
单片机学习笔记之点亮led 灯
随机推荐
Mongodb replication (replica set) summary
MongoDB优化的几点原则
Final review notes of single chip microcomputer principle
Xshell connection server changes key login to password login
LeetCode_二分搜索_中等_153.寻找旋转排序数组中的最小值
Some principles of mongodb optimization
《厌女:日本的女性嫌恶》摘录
Milkdown control icon
供应链供需预估-[时间序列]
最佳实践 | 用腾讯云AI意愿核身为电话合规保驾护航
Simple and easy-to-use code specification
RealBasicVSR测试图片、视频
Flink | multi stream conversion
实现IP地址归属地显示功能、号码归属地查询
2022-7-7 Leetcode 844.比较含退格的字符串
简单好用的代码规范
Read PG in data warehouse in one article_ stat
[learning notes] segment tree selection
我那“不好惹”的00后下属:不差钱,怼领导,抵制加班
.net core 关于redis的pipeline以及事务