当前位置:网站首页>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;
}
} 
边栏推荐
- OSI seven layer model
- Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf
- Mongodb slice summary
- 2022-7-6 Leetcode 977.有序数组的平方
- 一文读懂数仓中的pg_stat
- 信号强度(RSSI)知识整理
- xshell连接服务器把密钥登陆改为密码登陆
- 简单好用的代码规范
- Ways to improve the performance of raspberry pie
- Flink | 多流转换
猜你喜欢
![SSRF漏洞file伪协议之[网鼎杯 2018]Fakebook1](/img/10/6de1ee8467b18ae03894a8d5ba95ff.png)
SSRF漏洞file伪协议之[网鼎杯 2018]Fakebook1

ESP32构解工程添加组件

LED light of single chip microcomputer learning notes

2022-7-7 Leetcode 844.比较含退格的字符串

干货|总结那些漏洞工具的联动使用

【黑马早报】华为辟谣“军师”陈春花;恒驰5预售价17.9万元;周杰伦新专辑MV 3小时播放量破亿;法华寺回应万元月薪招人...

Getting started with MySQL

LIS longest ascending subsequence problem (dynamic programming, greed + dichotomy)

LeetCode简单题分享(20)

1、深拷贝 2、call apply bind 3、for of for in 区别
随机推荐
2022-7-6 初学redis(一)在 Linux 下下载安装并运行 redis
TPG x AIDU | AI leading talent recruitment plan in progress!
【等保】云计算安全扩展要求关注的安全目标和实现方式区分原则有哪些?
JS determines whether an object is empty
Esp32 construction engineering add components
MongoDB的导入导出、备份恢复总结
Realbasicvsr test pictures and videos
Introduction and basic use of stored procedures
MongoDB优化的几点原则
[daily training -- Tencent select 50] 231 Power of 2
交付效率提升52倍,运营效率提升10倍,看《金融云原生技术实践案例汇编》(附下载)
Why can basic data types call methods in JS
Write it down once Net a new energy system thread surge analysis
MongoDB的用户管理总结
【面试高频题】难度 2.5/5,简单结合 DFS 的 Trie 模板级运用题
[1] Basic knowledge of ros2 - summary version of operation commands
How to make join run faster?
OSI seven layer model
Some principles of mongodb optimization
2022-7-7 Leetcode 34.在排序数组中查找元素的第一个和最后一个位置