当前位置:网站首页>[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; }
边栏推荐
- An interview question combining defer and function in golang
- 919. 完全二叉树插入器 : 简单 BFS 运用题
- 【面试:并发篇23:多线程:join】join再理解
- FAW red flag "King fried" is listed, which is safe and comfortable
- Apple estimates that iPhone will give up the Chinese market, and the Chinese industrial chain needs to consider living a hard life
- Isn't it too much to play Gobang in idea?
- 接口测试工具 restlet client
- Per capita Swiss number series, Swiss number 4 generation JS reverse analysis
- Simple use of protobuf
- PE格式: 分析IatHook并实现
猜你喜欢

Creation and destruction of function stack frames

【Redis底层解析】链表类型

When facing complex problems, systematic thinking helps you understand the essence of the problem

Record the transfer of domain names from Alibaba cloud service providers to Huawei cloud

PE格式: 分析IatHook并实现
![[interview: concurrent 25: multithreading: volatile] visibility](/img/03/6b44242a019775222fdf9c7a920ae5.png)
[interview: concurrent 25: multithreading: volatile] visibility

【面试:并发篇23:多线程:join】join再理解

I'm also drunk. Eureka delayed registration and this pit!

How to implement distributed locks with redis?

SSH private key realizes login to remote target server
随机推荐
GDB locates the main address of the program after strip
新版Maixhub部署(V831与K210)
FAW red flag "King fried" is listed, which is safe and comfortable
工作面试总遇秒杀? 看了京东 T8 大咖私藏的秒杀系统笔记, 已献出膝盖
Ijcai2022 meeting! Microsoft and other tutorials on domain generalization
The inexplicability of Intel disassembly
IJCAI2022开会了! 微软等《领域泛化Domain Generalization》教程
GPON介绍及华为OLT网关注册配置流程
【C语言入门】ZZULIOJ 1016-1020
cuda_error_out_of_memory(out of memory怎么办)
PE format: analyze and implement IATHOOK
开源协议是否具有法律效力?
[MAIXPY]kpu: load error:2005, ERR_READ_FILE: read file failed问题解决
Autojs learning - file depth search
Oxford University: many common insomnia drugs lack long-term safety data
H5 realize the animation effect of a scratch card
When facing complex problems, systematic thinking helps you understand the essence of the problem
How to store pictures in the database "suggested collection"
我也是醉了,Eureka 延迟注册还有这个坑!
[manageengine]itsm application in retail industry
https://leetcode.cn/problems/middle-of-the-linked-list/submissions/


