当前位置:网站首页>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;
}
} 
边栏推荐
- 实现IP地址归属地显示功能、号码归属地查询
- SSRF漏洞file伪协议之[网鼎杯 2018]Fakebook1
- Getting started with MySQL
- Introduce six open source protocols in detail (instructions for programmers)
- JS slow motion animation principle teaching (super detail)
- LIS longest ascending subsequence problem (dynamic programming, greed + dichotomy)
- [daily training] 648 Word replacement
- Split screen bug notes
- DID登陆-MetaMask
- 【堡垒机】云堡垒机和普通堡垒机的区别是什么?
猜你喜欢

作战图鉴:12大场景详述容器安全建设要求

Final review notes of single chip microcomputer principle

C语言数组相关问题深度理解

Leecode3. Longest substring without repeated characters

Esp32 ① compilation environment

Build a secure and trusted computing platform based on Kunpeng's native security
![[Presto profile series] timeline use](/img/c6/83c4fdc5f001dab34ecf18c022d710.png)
[Presto profile series] timeline use

MySQL error 28 and solution

118. Yanghui triangle

Scrapy教程经典实战【新概念英语】
随机推荐
QQ medicine, Tencent ticket
最佳实践 | 用腾讯云AI意愿核身为电话合规保驾护航
Some principles of mongodb optimization
Oracle advanced (V) schema solution
QQ的药,腾讯的票
2022-7-6 初学redis(一)在 Linux 下下载安装并运行 redis
[learning notes] zkw segment tree
JNA learning notes 1: Concepts
Flink | 多流转换
SSRF漏洞file伪协议之[网鼎杯 2018]Fakebook1
How to make join run faster?
Write it down once Net a new energy system thread surge analysis
JS determines whether an object is empty
Sliding rail stepping motor commissioning (national ocean vehicle competition) (STM32 master control)
2022-7-6 Leetcode27.移除元素——太久没有做题了,为双指针如此狼狈的一天
Cinnamon taskbar speed
flask session伪造之hctf admin
Getting started with MySQL
MongoDB 分片总结
Read PG in data warehouse in one article_ stat