当前位置:网站首页>[notes for question brushing] delete continuous nodes with a total value of zero from the linked list
[notes for question brushing] delete continuous nodes with a total value of zero from the linked list
2022-07-29 01:00:00 【Qihai】
subject :
Give you a list of the head node head, Please write code , Delete from the linked list repeatedly The sum of the The value is 0 A sequence of continuous nodes , Until there is no such sequence .
After deleting , Please return the head node of the final result linked list .
You can return any answer that meets the requirements of the question .
( Be careful , All sequences in the following example , All right. ListNode Representation of object serialization .)
Example 1:
Input :head = [1,2,-3,3,1]
Output :[3,1]
Tips : answer [1,2,1] It's also true .
Example 2:Input :head = [1,2,3,-3,4]
Output :[1,2,4]
Example 3:Input :head = [1,2,3,-3,-2]
Output :[1]
Tips :
The list given to you may have 1 To 1000 Nodes .
For each node in the linked list , The value of the node :-1000 <= node.val <= 1000.source : Power button (LeetCode)
link :https://leetcode.cn/problems/remove-zero-sum-consecutive-nodes-from-linked-list
answer :
This question uses c The key to language is to grasp the two cycles .
Because it records the sum of consecutive , Then this interval can be ( Subscript is the benchmark , From 0 Start )0~1 0~2... 1~2..2~3... This also shows that we need to start from each node of the linked list as the head and look back . Then, naturally, an accumulated variable can be defined later , And also define a pointer that is easy to cycle later , When the sum is 0 It means that this interval adds up to 0, You can give up . Then the head of this interval is the previous next You can point to the end of this interval next 了 , This completes a rounding off of the interval .
In order to avoid the situation that the head node needs to be rounded off , So we need to create a node by ourselves , then next Point to the head node . The code is as follows :
struct ListNode* removeZeroSumSublists(struct ListNode* head){
// Create a head node , Prevent the header node from being deleted
struct ListNode* s = (struct ListNode*)malloc(sizeof(struct ListNode));
s->next = head;
struct ListNode* front = s;
int x = 0;
while(front)
{
struct ListNode* res = front->next;
while(res)
{
x += res->val;
if (x == 0)
{
front->next = res->next;
}
res = res->next;
}
x = 0;
front = front->next;
}
return s->next;
}
边栏推荐
- Selenium wire obtains Baidu Index
- B- 树 ~
- [raspberry pie] how does the windows computer connect with raspberry pie
- [Commons lang3 topic] 002 randomutils topic
- Five interesting magic commands in jupyter notebook
- 深度学习 | MATLAB实现TCN时间卷积神经网络spatialDropoutLayer参数描述
- Main thread and daemon thread
- Error reporting: the network preview shows {xxx:['this field is required']}
- Yield Guild Games:这一年的总结与未来展望
- NFT 项目的 7 种市场营销策略
猜你喜欢
小程序毕设作品之微信校园浴室预约小程序毕业设计成品(7)中期检查报告
Some considerations about ThreadPool
Meeting notification & meeting feedback & feedback details function of meeting OA project
华为发布HarmonyOS 3.0,向“万物互联”再迈一步
DRF -- authentication, authority, frequency source code analysis, global exception handling, automatic generation of interface documents, RBAC introduction
Wechat campus bathroom reservation applet graduation design finished product (5) assignment
面试突击69:TCP 可靠吗?为什么?
Talk about the cross end technical scheme
ThinkPHP高仿蓝奏云网盘系统程序
[untitled]
随机推荐
小程序毕设作品之微信校园浴室预约小程序毕业设计成品(8)毕业设计论文模板
机器学习 | MATLAB实现RBF径向基神经网络newrbe参数设定
追踪伦敦银实时行情的方法有哪些?
[web development] basic knowledge of flask framework
Wechat campus bathroom reservation applet graduation design finished product (5) assignment
将行内元素转换为块元素的方法
Daniel guild Games: summary and future outlook of this year
从零开始实现lmax-Disruptor队列(六)Disruptor 解决伪共享、消费者优雅停止实现原理解析
redis版本怎么查看(查看redis进程)
B+ 树 ~
ThinkPHP高仿蓝奏云网盘系统程序
Cloud function realizes website automatic check-in configuration details [web function /nodejs/cookie]
如何给女友讲明白JS的bind模拟实现
Jupyter notebook中5个有趣的魔法命令
分类预测 | MATLAB实现TCN时间卷积神经网络的时序分类预测
Anomaly detection and unsupervised learning (2)
【愚公系列】2022年07月 Go教学课程 020-Go容器之数组
day8
自制 | 纯手工自制一个16位RISC架构CPU
消费行业数字化升级成“刚需”,weiit新零售SaaS为企业赋能!