题目地址:https://leetcode.com/problems/remove-zero-sum-consecutive-nodes-from-linked-list/

题目描述

Given the head of a linked list, we repeatedly delete consecutive sequences of nodes that sum to 0 until there are no such sequences.

After doing so, return the head of the final linked list. You may return any such answer.

(Note that in the examples below, all sequences are serializations of ListNode objects.)

Example 1:

Input: head = [1,2,-3,3,1]
Output: [3,1]
Note: The answer [1,2,1] would also be accepted.

Example 2:

Input: head = [1,2,3,-3,4]
Output: [1,2,4]

Example 3:

Input: head = [1,2,3,-3,-2]
Output: [1]

Constraints:

  1. The given linked list will contain between 1 and 1000 nodes.
  2. Each node in the linked list has -1000 <= node.val <= 1000.

题目大意

删除一个链表中和为0的连续节点。

解题方法

preSum + 字典

如果是一个数组,我们删除其连续和为0的子数组怎么办?应该能想到preSum的方式,即累计preSum保存到字典中,如果preSum出现过,那么说明有一部分的和为0.

如果改成单链表,也可以使用同样的方式。使用字典保存preSum,如果遇到了已经出现过的preSum,那么需要根据上个preSum出现的位置,删除这段区间内的所有节点。

对于链表而言,删除其中的一段是很简单的,可以直接修改指针就行。但是只修改指针并没有修改字典,对于字典来说,我们也要删除被删除的哪些节点对应的preSum,为此,我们必须再次遍历被删除的这一段链表,计算preSum,并且从字典中删除。

由于头结点可能被删除,所以添加了一个dummy节点,把修改的链表放入dummy的后面。

C++代码如下:

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeZeroSumSublists(ListNode* head) {
unordered_map<int, ListNode*> m;
ListNode* dummy = new ListNode(-10000);
dummy->next = head;
int preSum = 0;
m[0] = dummy;
ListNode* cur = head;
while (cur) {
preSum += cur->val;
if (m.count(preSum)) {
ListNode* pre = m[preSum];
ListNode* cur = pre->next;
int p = preSum + cur->val;
while (p != preSum) {
m.erase(p);
cur = cur->next;
p += cur->val;
}
pre->next = cur->next;
} else {
m[preSum] = cur;
}
cur = cur->next;
}
return dummy->next;
}
};

参考资料:https://leetcode.com/problems/remove-zero-sum-consecutive-nodes-from-linked-list/discuss/366319/JavaC%2B%2BPython-Greedily-Skip-with-HashMap

日期

2019 年 9 月 27 日 —— 昨天面快手,竟然是纯刷题

【LeetCode】1171. Remove Zero Sum Consecutive Nodes from Linked List 解题报告 (C++)的更多相关文章

  1. 【leetcode】1171. Remove Zero Sum Consecutive Nodes from Linked List

    题目如下: Given the head of a linked list, we repeatedly delete consecutive sequences of nodes that sum ...

  2. leeetcode1171 Remove Zero Sum Consecutive Nodes from Linked List

    """ Given the head of a linked list, we repeatedly delete consecutive sequences of no ...

  3. 【LeetCode】1005. Maximize Sum Of Array After K Negations 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 日期 题目地址:https://leetco ...

  4. 【LeetCode】167. Two Sum II - Input array is sorted 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  5. 【LeetCode】653. Two Sum IV - Input is a BST 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 日期 题目地址:ht ...

  6. 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)

    [LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

  7. 【LeetCode】430. Flatten a Multilevel Doubly Linked List 解题报告(Python)

    [LeetCode]430. Flatten a Multilevel Doubly Linked List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...

  8. 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)

    [LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...

  9. 【LeetCode】154. Find Minimum in Rotated Sorted Array II 解题报告(Python)

    [LeetCode]154. Find Minimum in Rotated Sorted Array II 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...

随机推荐

  1. Hermite WENO 重构格式

    Hermite WENO 单元重构 本文主要介绍采用 Hermite WENO 重构方法作为斜率限制器应用于二维或高维单元中. 1.简介[1] ENO格式最早由 Harten 等[2]提出,ENO格式 ...

  2. Excel-数据分列的多种方法实现

    2.数据->分列 (数据格式统一的精准分列)<=> 手动快捷键ctrl+E+等待 ("模糊模仿""分列)<=> 用函数实现(精准分列) 用函 ...

  3. nit是虱子的卵

    如题.[牛津] (egg of a) louse or other parasitic insect 虱或其他寄生虫(的卵). 忘了在那个帖子里说nit: 虱子了. 为了凑字数,迄今为止六级/考研单词 ...

  4. Spark(六)【RDD的血缘依赖】

    RDD依赖关系 1. RDD血缘关系 ​ RDD只支持粗粒度转换,即在大量记录上执行的单个操作.将创建RDD的一系列Lineage(血统)记录下来,以便恢复丢失的分区.RDD的Lineage会记录RD ...

  5. flink---实时项目--day01--1. openrestry的安装 2. 使用nginx+lua将日志数据写入指定文件中 3. 使用flume将本地磁盘中的日志数据采集到的kafka中去

    1. openrestry的安装 OpenResty = Nginx + Lua,是⼀一个增强的Nginx,可以编写lua脚本实现⾮非常灵活的逻辑 (1)安装开发库依赖 yum install -y ...

  6. Playing with Destructors in C++

    Predict the output of the below code snippet. 1 #include <iostream> 2 using namespace std; 3 4 ...

  7. Actuator监控器

    一.简介 Actuator(激励者;执行器)是Spring Boot提供的一个可挺拔模块,用于对工程进行监控.其通过不同的监控终端实现不同的监控功能.其功能与Dubbo的监控中心类似,不同的是,Dub ...

  8. js中获取url参数

    function getUrlVars() { var vars = [], hash; var hashes = window.location.href.slice(window.location ...

  9. 使用buffered流结合byte数组,读入文件中的内容,包括中文字符

    package com.itcast.demo05.Buffered;import java.io.BufferedInputStream;import java.io.FileInputStream ...

  10. NGNIX 开启socket分发的使用配置

    worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/err ...