当前位置:网站首页>[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从入门到入土(24)提取指令网卡的ip
- The concept and operation rules of calculus of variations
- 软件测试 -- 1 软件测试知识大纲梳理
- gson与fastjson
- English grammar_ Indefinite pronoun - other / other
- 应用实践:Paddle分类模型大集成者[PaddleHub、Finetune、prompt]
- 国联证券买股票开户安全吗?
- Thymeleaf controls whether display is displayed through style
- IP地址分类,判断一个网段是子网超网
- I2C设备驱动程序的层次结构
猜你喜欢

Filters get the data in data; Filters use data in data
![优质数对的数目[位运算特点+抽象能力考察+分组快速统计]](/img/c9/8f8f0934111f7ae8f8abd656d92f12.png)
优质数对的数目[位运算特点+抽象能力考察+分组快速统计]

Why do China Construction and China Railway need this certificate? What is the reason?

43 盒子模型

44 Sina navigation, Xiaomi sidebar exercise

Deng Qinglin, a technical expert of Alibaba cloud: Best Practices for disaster recovery and remote multi activity across availability zones on cloud

GameFramework制作游戏(二)制作UI界面

filters获取data中的数据;filters使用data中的数据

06、类神经网络

27 classification of selectors
随机推荐
Pytorch training code writing skills, dataloader, Einstein logo
Why do China Construction and China Railway need this certificate? What is the reason?
Overview of cloud security technology development
SSM framework integration, simple case
Realsense-Ros安装配置介绍与问题解决
SSH服务器拒绝了密码
MySQL 45讲 | 06 全局锁和表锁 :给表加个字段怎么有这么多阻碍?
IP address classification, which determines whether a network segment is a subnet supernetwork
44 Sina navigation, Xiaomi sidebar exercise
转载----如何阅读代码?
The supply chain collaborative management system, a new "engine" of digitalization in machinery manufacturing industry, helps enterprises' refined management to a new level
EDA chip design solution based on AMD epyc server
35 快速格式化代码
[MySQL series] - how much do you know about the index
[C题目]力扣206. 反转链表
Polymorphism and interface
Dpkg package download addresses of various platforms (including arm64)
easygui使用的语法总结
[nuxt 3] (XI) transmission & module
基于浏览器的分屏阅读