当前位置:网站首页>[summary of linked list skills] 876. Intermediate node of linked list
[summary of linked list skills] 876. Intermediate node of linked list
2022-07-22 19:00:00 【Sand is sand】
876. The middle node of a linked list
One 、 subject
Given a header node as head The non empty single chain table of , Returns the middle node of the linked list .
If there are two intermediate nodes , Then return to the second intermediate node .
Example 1:
Input :[1,2,3,4,5]
Output : Nodes in this list 3 ( Serialization form :[3,4,5])
The returned node value is 3 . ( The evaluation system expresses the serialization of this node as follows [3,4,5]).
Be careful , We returned one ListNode Object of type ans, such :
ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, as well as ans.next.next.next = NULL.
Example 2:
Input :[1,2,3,4,5,6]
Output : Nodes in this list 4 ( Serialization form :[4,5,6])
Because the list has two intermediate nodes , Values, respectively 3 and 4, Let's go back to the second node .
Tips :
The number of nodes in a given list is between 1 and 100 Between .
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/middle-of-the-linked-list
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Two 、 Problem solving
Ideas :
The key to the problem is that we can't get the length of the single linked list directly
n
, The conventional method is to traverse the linked list firstn
, Traverse again to get the secondn / 2
Nodes , That is, the intermediate node .If you want to traverse once, you can get the intermediate node , You also need to be smart , Use 「 Speed pointer 」 The technique of :
Let's make two pointers
slow
andfast
Respectively point to the chain header nodehead
.Whenever the pointer is slow
slow
Take a step forward , Quick pointerfast
Just two steps forward , such , Whenfast
When you get to the end of the linked list ,slow
It points to the midpoint of the linked list .
ListNode middleNode(ListNode head) {
// The speed pointer initializes to head
ListNode slow = head, fast = head;
// The pointer stops when it comes to the end
while (fast != null && fast.next != null) {
// Slow pointer one step , Let's go two steps
slow = slow.next;
fast = fast.next.next;
}
// The slow pointer points to the midpoint
return slow;
}
It should be noted that , If the length of the list is even , That is to say, when there are two midpoint , The node returned by our solution is the later node .
Force to buckle 876 topic 「 The middle node of a list 」 You can directly apply this solution .
in addition , With a little modification, this code can be directly used to judge the algorithm problem of linked list forming a ring .
Code :
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
It should be noted that , If the length of the list is even , That is to say, when there are two midpoint , The node returned by our solution is the later node .
*/
class Solution {
public:
ListNode* middleNode(ListNode* head) {
ListNode* slow = head;
ListNode* fast = head;
while(fast != NULL&&fast->next != NULL){
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
};
边栏推荐
- leetCode笔记
- To achieve the effect of budget DLL, QT embeds the third-party window into the program, excel operation, database foreign keys, and determines whether the program is started
- 【滑动窗口技巧】76. 最小覆盖子串
- PCV、PIL、Pillow安装
- 1. Qimage filling transparent brush; 2.path.addtext how to add line breaks
- numpy.reshape完成图像切割
- 【链表技巧汇总】141.环形链表(简单)
- Leetcode 2028. find out the missing observation data
- 1. A complete instance of movetothread, 2. QT expression evaluation
- BigDecimal中除法divide()方法的详细解析,带你走进源码的world
猜你喜欢
MySQL statement execution order
Wechat scans the QR code of the website, but only displays the link address, and cannot jump to the web page
Learning to Incorporate Structure Knowledge for Image Inpainting
NRF24L01 wireless module setting transmit receive mode method
Vlfeat, pydot configuration
Flink learning notes (V) datastream API
mysql主从复制
VLFeat、pydot配置
6-2-depth first search underground maze exploration (30 points)
Recursively find the partial sum of simple alternating power series (15 points)
随机推荐
Leetcode 2039. when the network is idle
程序员面试金典面试题 01.02. 判定是否互为字符重排
1.常量中有换行符Qt5-》vs的解决方案;2.同一份代码Qt和vs共同编译的问题和解决方案
Wechat scans the QR code of the website, but only displays the link address, and cannot jump to the web page
paper - A Physics-based Noise Formation Model for Extreme Low-light Raw Denoising
Leetcode 2028. find out the missing observation data
程序员面试金典面试题 01.05. 一次编辑
Summary of all usage of join in SQL syntax (simple example)
JVM调优实战-从零开始 | 项目有关JVM调优总结
Six dimensional space
PAT乙级1019 数字黑洞 (20 分) (测点错误可能原因)
Parameter index out of range (1 > number of parameters, which is 0).
PTA basic question 7-23 currency conversion (20 points) (true)
JVM-系统优化
pytorch自定义dataloder的时候,返回参数
各种技术资料汇总-MYSQL
6-2-depth first search underground maze exploration (30 points)
项目启动过后,一直循环加载mapper xml文件
Exercise 7-4 find out the elements that are not common to two arrays (C language)
Vlfeat, pydot configuration