当前位置:网站首页>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;
}
}
边栏推荐
- Enregistrement de la navigation et de la mise en service du robot ROS intérieur (expérience de sélection du rayon de dilatation)
- clion mingw64中文乱码
- 118. 杨辉三角
- LeetCode_ Binary search_ Medium_ 153. Find the minimum value in the rotation sort array
- Ogre introduction
- 2022-7-7 Leetcode 34.在排序数组中查找元素的第一个和最后一个位置
- 交付效率提升52倍,运营效率提升10倍,看《金融云原生技术实践案例汇编》(附下载)
- Digital IC Design SPI
- MySQL error 28 and solution
- 供应链供需预估-[时间序列]
猜你喜欢
Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf
Digital IC Design SPI
118. 杨辉三角
[dark horse morning post] Huawei refutes rumors about "military master" Chen Chunhua; Hengchi 5 has a pre-sale price of 179000 yuan; Jay Chou's new album MV has played more than 100 million in 3 hours
Navicat运行sql文件导入数据不全或导入失败
Xshell connection server changes key login to password login
室內ROS機器人導航調試記錄(膨脹半徑的選取經驗)
Cmake learning and use notes (1)
Esp32 ① compilation environment
ESP32构解工程添加组件
随机推荐
JS determines whether an object is empty
[QNX Hypervisor 2.2用户手册]6.3.4 虚拟寄存器(guest_shm.h)
2022-7-6 Leetcode 977.有序数组的平方
Introduction and basic use of stored procedures
Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf
Leecode3. Longest substring without repeated characters
Pcap learning notes II: pcap4j source code Notes
Thread pool reject policy best practices
Flink | 多流转换
[QNX hypervisor 2.2 user manual]6.3.4 virtual register (guest_shm.h)
clion mingw64中文乱码
OSI 七层模型
室內ROS機器人導航調試記錄(膨脹半徑的選取經驗)
记一次 .NET 某新能源系统 线程疯涨 分析
Digital IC Design SPI
cmake 学习使用笔记(一)
【堡垒机】云堡垒机和普通堡垒机的区别是什么?
QQ的药,腾讯的票
LeetCode_二分搜索_中等_153.寻找旋转排序数组中的最小值
Scrapy教程经典实战【新概念英语】