当前位置:网站首页>[C topic] force buckle 876. Intermediate node of linked list
[C topic] force buckle 876. Intermediate node of linked list
2022-07-25 14:50:00 【GLC8866】
Method 1 : Calculate the number of nodes count, Move count/2 The second is the node required by the topic .
struct ListNode* middleNode(struct ListNode* head)
{
struct ListNode* cur=head;// Mark the current node
int count=0;//count Record the number of nodes
while(cur)// Calculate the number of nodes
{
count++;
cur=cur->next;
}
cur=head;// Back to the head node
for(int i=0;i<(count/2);i++)// The first cycle ,cur For the first time 2 Nodes . The first (count/2)+1 Nodes , Need to cycle count/2 Time .
{
cur=cur->next;
}
return cur;
}Method 2 : Speed pointer .slow and fast The starting points are head,slow Move backward one node at a time ,fast Move back two nodes at a time .fast Moving two nodes backward is called fast->next->next assignment , This premise is fast->next Not for NULL, and fast->next Not for NULL The premise is fast Not for NULL. Therefore, the cycle judgment condition is (fast&&fast->next), also fast Must be written in fast->next Left side , When the number of nodes is even ,slow After reaching the intermediate node ,fast by NULL, Because logical operators && Will control the evaluation order , They don't execute fast->next, perform fast->next Will dereference null pointers .
( The picture shows that , When the number of nodes is odd ,slow After reaching the intermediate node fast->next by NULL, End cycle . When the number of nodes is even ,slow After reaching the intermediate node fast by NULL, End cycle . Find the intermediate node and return .)
struct ListNode* middleNode(struct ListNode* head)
{
struct ListNode* fast=head;
struct ListNode* slow=head;
while(fast&&fast->next)
{
slow=slow->next;
fast=fast->next->next;
}
return slow;
}边栏推荐
- Awk from getting started to digging in (21) awk script debugging
- Sudo rosdep init error ROS installation problem solution
- 27 classification of selectors
- Ssh server rejected password
- 转载----如何阅读代码?
- Awk from entry to earth (24) extract the IP of the instruction network card
- PHP 通过原生CURL实现非阻塞(并发)请求模式
- Deng Qinglin, a technical expert of Alibaba cloud: Best Practices for disaster recovery and remote multi activity across availability zones on cloud
- 32 use of chrome debugging tools
- Why do China Construction and China Railway need this certificate? What is the reason?
猜你喜欢

44 Sina navigation, Xiaomi sidebar exercise

D2. Chopping Carrots (Hard Version) (每日一题)

45padding不会撑开盒子的情况

easygui使用的语法总结

Browser based split screen reading
![[eloquence] negotiation persuasion skills and Strategies](/img/01/0fcbbfcf35a6d166c14ba4215eacac.jpg)
[eloquence] negotiation persuasion skills and Strategies

41 picture background synthesis - colorful navigation map

LeetCode-198-打家劫舍

Idea error failed to determine a suitable driver class

English grammar_ Indefinite pronoun - other / other
随机推荐
应用实践:Paddle分类模型大集成者[PaddleHub、Finetune、prompt]
Niuke multi school E G J L
English语法_不定代词 - other / another
LeetCode_字符串_中等_151.颠倒字符串中的单词
[Nuxt 3] (十一) 传送 & 模块
Realsense ROS installation configuration introduction and problem solving
Go语言创始人从Google离职
PHP 通过原生CURL实现非阻塞(并发)请求模式
Leetcode-198- house raiding
Melodic + Realsense D435i 配置及错误问题解决
Quickly set up dobbo demo
GameFramework制作游戏(一)
Browser based split screen reading
Several methods of spark parameter configuration
没错,请求DNS服务器还可以使用UDP协议
awk从入门到入土(21)awk脚本调试
[C题目]力扣876. 链表的中间结点
Why do China Construction and China Railway need this certificate? What is the reason?
Software testing -- 1. Outline of software testing knowledge
Wechat official account official environment online deployment, third-party public platform access