当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢

「Gonna Be Alright 会好的」数藏现已开售!感受艺术家的心灵共鸣

零基础小白也能懂的 Redis 数据库,手把手教你易学易用!

JMeter interface test (login, registration)

小于等于K的最大子数组累加和

VR全景人淘金“小心机”(上)

Function pointer and callback function

Interview question: the difference between three instantiated objects in string class

手动从0搭建ABP框架-ABP官方完整解决方案和手动搭建简化解决方案实践

【愚公系列】2022年7月 Go教学课程 018-分支结构之switch

452页13万字现代智慧乡镇雪亮工程整体解决方案2022版
随机推荐
Case when in MySQL returns multiple field processing schemes
Analyze CAS written by CSDN boss, re-entry lock, unfair lock
暑假加餐|有钱人和你想的不一样(第5天)+电力系统潮流仿真(文档和Matlab代码)
The job created by flinksqlclient will disappear after the restart of Flink. Is there any way?
《MySQL》认识MySQL与计算机基础知识
小于等于K的最大子数组累加和
jmeter接口测试(登录、注册)
Restful fast request 2022.2.2 release, supporting batch export of documents
Lixia action | Yuanqi Digitalization: existing mode or open source innovation?
A. YES or YES?
Have you encountered the situation that CDC reads incomplete MySQL fields? How to deal with it?
04. Detailed steps for installing the simulated browser chromedriver in Google browser
Subject 3: Jinan Zhangqiu line 6
Detailed analysis of trajectory generation tool in psins toolbox
开机启动流程及营救模式
C language introduction practice (12): find the value of natural constant e
一文读懂 | 数据中台如何支撑企业数字化经营
[OBS] dynamic bit rate: bit rate estimation
222. 完全二叉树的节点个数
Golang sends email to the mail Library