当前位置:网站首页>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;
}
}
边栏推荐
- High end for 8 years, how is Yadi now?
- Signal strength (RSSI) knowledge sorting
- 作战图鉴:12大场景详述容器安全建设要求
- 【堡垒机】云堡垒机和普通堡垒机的区别是什么?
- PAcP learning note 3: pcap method description
- 靠卖概念上市,认养一头牛能走多远?
- Xshell connection server changes key login to password login
- ESP32系列专栏
- "Song of ice and fire" in the eleventh issue of "open source Roundtable" -- how to balance the natural contradiction between open source and security?
- Drawerlayout suppress sideslip display
猜你喜欢
[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
数据库系统概论-第一章绪论【概念模型、层次模型和三级模式(外模式、模式、内模式)】
我那“不好惹”的00后下属:不差钱,怼领导,抵制加班
Detr introduction
分布式事务解决方案
Introduce six open source protocols in detail (instructions for programmers)
10 pictures open the door of CPU cache consistency
[Presto profile series] timeline use
Error lnk2019: unresolved external symbol
xshell连接服务器把密钥登陆改为密码登陆
随机推荐
Esp32 construction engineering add components
Digital IC Design SPI
【堡垒机】云堡垒机和普通堡垒机的区别是什么?
分布式事务解决方案
C语言数组相关问题深度理解
High end for 8 years, how is Yadi now?
2022-7-6 Leetcode 977.有序数组的平方
Fast development board pinctrl and GPIO subsystem experiment for itop-imx6ull - modify the device tree file
Why can basic data types call methods in JS
Realize the IP address home display function and number home query
10 pictures open the door of CPU cache consistency
数字ic设计——SPI
【面试高频题】难度 2.5/5,简单结合 DFS 的 Trie 模板级运用题
Realbasicvsr test pictures and videos
【日常训练--腾讯精选50】231. 2 的幂
toRaw和markRaw
Indoor ROS robot navigation commissioning record (experience in selecting expansion radius)
move base参数解析及经验总结
MongoDB 遇见 spark(进行整合)
2022-7-7 Leetcode 34.在排序数组中查找元素的第一个和最后一个位置