当前位置:网站首页>[leetcode ladder] linked list · 876 find the middle node of the linked list
[leetcode ladder] linked list · 876 find the middle node of the linked list
2022-07-25 21:41:00 【kikokingzz】

Title Description :
Given a header node as
headThe 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 .Topic link :
Their thinking :
Conventional method 1: Speed pointer
This is a classic topic , Use the speed pointer . Among them, the fast pointer takes two steps at a time , Slow pointer one step at a time , When the fast pointer goes empty , The slow pointer is just halfway .
struct ListNode* middleNode(struct ListNode* head){ struct ListNode* slow=head; if(head==NULL) // If it is an empty list , that fast It didn't exist at first return NULL; struct ListNode* fast=head->next; while(fast) { fast=fast->next; // Whether it is an even number of nodes or an odd number of nodes ,fast There will be no null pointer in the penultimate step of if(fast!=NULL) // At the last step ,fast May already be empty , At this time, there is no need to take another step backwards fast=fast->next; slow=slow->next; //slow One step back at a time } return slow; }
To sum up : Here we use the operation of fast and slow pointer to find the intermediate node , But there are two aspects of this method that deserve attention :
- The first point is when the original linked list is an empty linked list , I can't find it fast The position of the initial pointer of , If directly executed fast=head->next; Null pointer errors may occur .
- The second point is in the process of loop traversal ,fast The pointer enters in the last two steps NULL when , When the original linked list has an even number of nodes , The penultimate step has entered NULL 了 , At this point, if you directly execute the second fast=fast->next; Null pointer errors still occur .
So is there any way to simplify this operation ? That's for sure !
Conventional method 2: Improved version of fast and slow pointer
In fact, we can improve the two characteristics of the first method , The first is the first point , At the beginning , We might as well fast Pointers and slow All point to head, In this way, you don't have to specially consider the case that the original linked list is empty ,fast Point to NULL->next situation . But changed this , At the same time, we need to modify the end condition of the loop , We can find even and odd nodes by plotting slow When pointing to the intermediate node fast End position :
- When the number of linked list nodes is odd :fast->next by NULL End traversal .
- When the number of linked list nodes is even :fast by NULL End traversal .
struct ListNode* middleNode(struct ListNode* head){ struct ListNode* slow = head; // The speed pointer points to the first node of the linked list struct ListNode* fast = head; while(fast&&fast->next) // As long as one of these two is satisfied NULL, explain slow It points to the middle node of the linked list { slow=slow->next; fast=fast->next->next; } return slow; }
边栏推荐
- 再次来光顾
- Detailed explanation of Ag search tool parameters
- The noise reduction effect is increased by more than 6 times! With the microphone inside the headset, this wireless noise reduction headset is unusual!
- Trusted and controllable way of Tencent cloud database
- zigbee物联网开发平台(工业物联网)
- [ManageEngine] value brought by Siem to enterprises
- The adequacy of source evaluation forum · observation model test
- How to store pictures in the database "suggested collection"
- Protobuf的简单使用
- Research: more than 70% of doctors are still prescribing unsafe antibiotic drugs
猜你喜欢

【Redis底层解析】字符串类型

2022 latest examination questions and answers of eight members (standard staff) of Shanghai Architecture
![[ManageEngine] value brought by Siem to enterprises](/img/1e/56d64d193e0428523418bef5e98866.png)
[ManageEngine] value brought by Siem to enterprises

Sentinel vs Hystrix 限流对比,到底怎么选?

腾讯云数据库的可信可控之路

How to implement distributed locks with redis?

【leetcode天梯】链表 · 876 查找链表中间结点

Huawei occupies half of the folding mobile phone market, proving its irreplaceable position in the high-end market

Optimization analysis of storage structure and IO performance of openharmony littlefs file system

GPON介绍及华为OLT网关注册配置流程
随机推荐
Airtest solves the problem that a password needs to be entered in the process of "automatic packaging" (the same applies to random bullet frame processing)
选择的能力
如何自动生成短链?如何在线批量生成带UTM参数的链接?
新版Maixhub部署(V831与K210)
五、品达通用权限系统__pd-tools-xxs(防跨站脚本攻击)
On Web Performance Optimization (1)
Reading the pointpillar code of openpcdet -- Part 3: Calculation of loss function
Per capita Swiss number series, Swiss number 4 generation JS reverse analysis
Apple estimates that iPhone will give up the Chinese market, and the Chinese industrial chain needs to consider living a hard life
I'm also drunk. Eureka delayed registration and this pit!
All non isomorphic subgraphs of a directed complete graph of order 3 (number of different hook graphs)
Web3 entrepreneurship has all the elements of explosive growth of innovation
Detailed explanation of JVM memory model and structure (five model diagrams)
GDB locates the main address of the program after strip
Oxford University: many common insomnia drugs lack long-term safety data
[JS] the problem pointed by this
Interviewer of large factory: how to quickly query a table with tens of millions of data?
C#常见的集合
Advanced technology management - how can the team be broken?
性能调试 -- Chrome Performance



