当前位置:网站首页>Daily question 1: delete continuous nodes with a total value of zero from the linked list
Daily question 1: delete continuous nodes with a total value of zero from the linked list
2022-07-27 04:08:00 【Sharp blade CC】
1171. Delete consecutive nodes with zero sum from the linked list
Medium difficulty
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 Of Continuous node The sequence of components , 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]
Violence solution :
If you want to traverse to each group, the sum is equal to 0 Continuous node of , You can start from each node , Traverse its suffix and , If its suffix sum equals 0 了 , Specify the starting node of the current traversal to make the suffix sum equal to 0 These nodes of are a set of summation equals 0 Continuous node of , Should be deleted , But don't delete, Because after testing, if delete After the U-turn node Leetcode Will report a mistake , Guess it might be related to Leetcode The implementation of the linked list of test cases is related , So the way to delete it is cur->next = search->next, here cur Is the previous node of the starting node ,search Is to make the prefix and equal 0 The node of .
In order to avoid the difficulty of returning to a new head node after the head node is deleted , At the same time, it can be matched with the idea of the previous node of the starting node , You can add a sentinel node newhead.
Code :
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next) {} * }; */
class Solution {
public:
ListNode* removeZeroSumSublists(ListNode* head) {
// Create a head node
ListNode* newhead = new ListNode(0, head);
// Create a cur Used as the starting node of each traversal
ListNode* cur = newhead;
while(cur != nullptr)
{
int sum = 0;
ListNode* search = cur->next;
while(search != nullptr)
{
sum += search->val;
if(sum == 0)
{
cur->next = search->next;
}
search = search->next;
}
cur = cur->next;
}
ListNode* tmp = newhead->next;
delete newhead;
return tmp;
}
};
边栏推荐
- Case when in MySQL returns multiple field processing schemes
- 注释有点好玩哦
- NFT数字藏品系统开发:小蚁数智帮助品牌一键上链发行NFT
- 0726~简历梳理面试总结
- Want to get the Apache official domain name mailbox? Exclusive interview with Apache linkis five new committers to tell you how to do it
- NFT数字藏品系统开发:老牌文学杂志玩起新潮数字藏品
- 路由策略第一关
- Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community
- Chapter 5 decision tree and random forest practice
- 【愚公系列】2022年7月 Go教学课程 018-分支结构之switch
猜你喜欢

An online duplicate of a hidden bug

Framework学习之旅:init 进程启动过程
![Abstract intelligent extraction [based on Bert technology]](/img/1c/7c1b0e9bc9af62308f4124104f6110.png)
Abstract intelligent extraction [based on Bert technology]

Restful fast request 2022.2.2 release, supporting batch export of documents

Plato farm is expected to further expand its ecosystem through elephant swap
![[Android synopsis] kotlin multithreaded programming (I)](/img/04/4349bacbd401868d73a3b05d018b66.png)
[Android synopsis] kotlin multithreaded programming (I)

288 page 180000 word intelligent campus overall design directory

Chapter 4 决策树和随机森林

3381. 手机键盘(清华大学考研机试题)

NFT数字藏品系统开发:老牌文学杂志玩起新潮数字藏品
随机推荐
第五届强网杯全国网络安全挑战赛 题目复现(有题目附件,详解)
什么是动画效果?什么是过渡效果?
Skywalking distributed system application performance monitoring tool - medium
H.265网页播放器EasyPlayer对外开放录像的方法
大家有遇到CDC读MySQL字段不全的情况吗?怎么处理的?
The problem that prevented the installation of umi4 for one day has been solved
jmeter接口测试(登录、注册)
括号的最大嵌套深度
Alibaba cloud server domain name and port web page cannot access the problem record
C language introduction practice (12): find the value of natural constant e
【obs】动态码率:码率估算
"Gonna be right" digital collection is now on sale! Feel the spiritual resonance of artists
Application of one-dimensional array
222. 完全二叉树的节点个数
C语言学习笔记 —— 内存管理
《MySQL》认识MySQL与计算机基础知识
Golang发送邮件库email
Golang sends email to the mail Library
The 100th of the commercial anti counterfeiting series - boring systems and management processes can really be thrown into the trash can - by the way, analyze a dozen useless unity game self-test proj
三种常见的移动底盘运动学模型分析