当前位置:网站首页>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;
}
}
边栏推荐
- SSRF漏洞file伪协议之[网鼎杯 2018]Fakebook1
- Vscode编辑器ESP32头文件波浪线不跳转彻底解决
- Getting started with MySQL
- High end for 8 years, how is Yadi now?
- What parameters need to be reconfigured to replace the new radar of ROS robot
- QQ的药,腾讯的票
- 数据库系统概论-第一章绪论【概念模型、层次模型和三级模式(外模式、模式、内模式)】
- [QNX Hypervisor 2.2用户手册]6.3.4 虚拟寄存器(guest_shm.h)
- OSI seven layer model
- LED light of single chip microcomputer learning notes
猜你喜欢
数据库系统概论-第一章绪论【概念模型、层次模型和三级模式(外模式、模式、内模式)】
Error lnk2019: unresolved external symbol
Getting started with MySQL
Flink | 多流转换
OSI 七层模型
《厌女:日本的女性嫌恶》摘录
Fast development board pinctrl and GPIO subsystem experiment for itop-imx6ull - modify the device tree file
ESP32构解工程添加组件
Cinnamon taskbar speed
室内ROS机器人导航调试记录(膨胀半径的选取经验)
随机推荐
2022-7-6 使用SIGURG来接受外带数据,不知道为什么打印不出来
干货|总结那些漏洞工具的联动使用
一文读懂数仓中的pg_stat
10 pictures open the door of CPU cache consistency
【日常训练】648. 单词替换
[daily training] 648 Word replacement
Enregistrement de la navigation et de la mise en service du robot ROS intérieur (expérience de sélection du rayon de dilatation)
Getting started with MySQL
Some principles of mongodb optimization
Scrapy教程经典实战【新概念英语】
Ikvm of toolbox Net project new progress
mysql 局域网内访问不到的问题
[learning notes] segment tree selection
Milkdown control icon
How far can it go to adopt a cow by selling the concept to the market?
提升树莓派性能的方法
ROS机器人更换新雷达需要重新配置哪些参数
Scripy tutorial classic practice [New Concept English]
Solve the cache breakdown problem
Signal strength (RSSI) knowledge sorting