当前位置:网站首页>Leetcode daily practice - 876. Intermediate node of linked list
Leetcode daily practice - 876. Intermediate node of linked list
2022-07-27 21:07:00 【An airliner flying to the stars】
Preface
Wassup guys! I am a Edison
It's today LeetCode Upper leetcode 876. The middle node of a list
Let’s get it!

List of articles
1. Topic analysis
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:
Example 2:
2. Title diagram
The method used in this problem is Fast and slow pointer method ;
(1) Define slow pointer slow One go 1 Step ;
(2) Define fast pointers fast One go 2 Step ;
Odd numbers
When the linked list is Odd number Time , First ,slow and fast All point to the first node ( As shown in the figure )
then slow One go 1 Step ,fast One go 2 Step , Dynamic diagram demonstration 
When fast Go to the Tail node When ,slow It points to Intermediate nodes
Even numbers
When the linked list is even numbers Time , First ,slow and fast All point to the first node ( As shown in the figure )
then slow One go 1 Step ,fast One go 2 Step , Dynamic diagram demonstration 
You can see , When fast Go to the NULL when ,slow It points to Intermediate nodes ;
summary :
(1) When the number of nodes in the linked list is Odd number when , The condition for the end of the loop is fast Go to the Tail node ;
(2) When the number of nodes in the linked list is even numbers when , The condition for the end of the loop is fast Go to the NULL;
3. Code implementation
Interface code
struct ListNode* middleNode(struct ListNode* head){
struct ListNode* slow, *fast;
slow = fast = head
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
Submit results 
One more thing to note : stay while Loop judgment section , It's a condition for continuation , Think about the conditions for the end !
边栏推荐
- R语言dplyr包summarise_at函数计算dataframe数据中多个数据列(通过向量指定)的计数个数、均值和中位数、使用list函数指定函数列表(使用.符号和~符号指定函数语法purr)
- LeetCode每日一练 —— 203. 移除链表元素
- MySQL design optimization generates columns
- Academic sharing | Tsinghua University, Kang Chongqing: power system carbon measurement technology and application (matlab code implementation)
- CPDA|如何拥有数据分析思维?
- 【R语言】【1】初学R语言语法使用Rstudio编辑
- MapGIS三维场景渲染技术与应用
- What are the application scenarios of real name authentication in the cultural tourism industry?
- [numpy] array index and slice
- Digital leading planning first, focusing on the construction of intelligent planning information platform and the exploration and practice of application projects
猜你喜欢

自动化测试----unittest框架

Airiot Q & A issue 6 | how to use the secondary development engine?
![[program life]](/img/68/9a337d37490ad5cd75095cc5efd5e4.jpg)
[program life] "stage summary" - unwilling to be ordinary

Typoa spelling check: missing dictionary file for Chinese

Introduction to source insight 4.0

Where is the program?

Automated testing ----- selenium (II)

二舅,为什么火了?
![[numpy] array index and slice](/img/ce/34db7aef3fefe8a03e638d0838492f.png)
[numpy] array index and slice

NPDP | what kind of product manager can be called excellent?
随机推荐
Hexagon_V65_Programmers_Reference_Manual(5)
自动化测试----selenium(二)
Uncaught SyntaxError: redeclaration of let page
Opencv implements image clipping and scaling
Automated testing - unittest framework
Do you know about data synchronization?
命令行 PDF 转换器:::fCoder 2PDF
QT OpenGL makes objects move under care to form animation
How to translate the address in the program?
Repeated DNA sequence [hash determination repetition + sliding window + bit operation of binary coding]
A lock faster than read-write lock. Don't get to know it quickly
Hexagon_V65_Programmers_Reference_Manual(8)
[deep learning] pytoch torch Autograd automatic differential engine
DJI push code (one code for one use, updated on July 26, 2022)
一文读懂Plato Farm的ePLATO,以及其高溢价缘由
[today in history] July 27: model testing pioneer was born; Microsoft acquires qdos; The first laser typesetting Chinese newspaper
云管平台中租户以及多租户概念简单说明
北京/上海/广州/深圳DAMA-CDGA/CDGP数据治理认证报名条件
Beijing / Shanghai / Guangzhou / Shenzhen dama-cdga/cdgp data governance certification registration conditions
Codeforces 1706E 并查集 + 启发式合并 + ST 表