题目如下:

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:

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

解题思路:方法比较简单,可以从头开始遍历链表,并累加每一个节点的和,遇到和为零或者与之前出现过的和相同的情况则表示这一段可以删除,删除后又重头开始遍历链表,直到找不到可以删除的段为止。至于怎么删除链表的中一段,我的方法是用一个list保存链表的所有节点,删除的时候直接删除list的元素。全部删除完成后再将list的剩余元素组成新链表即可。

代码如下:

# Definition for singly-linked list.
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None class Solution(object):
def removeZeroSumSublists(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
node_list = []
node = head
while node != None:
node_list.append(node)
node = node.next flag = True
while flag:
flag = False
dic = {}
amount = 0
for inx,item in enumerate(node_list):
amount += item.val
if amount == 0:
node_list = node_list[inx+1:]
flag = True
break
elif amount in dic:
node_list = node_list[:dic[amount] + 1] + node_list[inx + 1:]
flag = True
break
dic[amount] = inx
if len(node_list) == 0:
return None
for i in range(len(node_list)-1):
node_list[i].next = node_list[i+1]
node_list[-1].next = None
head = node_list[0]
return head

【leetcode】1171. Remove Zero Sum Consecutive Nodes from Linked List的更多相关文章

  1. 【LeetCode】1171. Remove Zero Sum Consecutive Nodes from Linked List 解题报告 (C++)

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

  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】659. Split Array into Consecutive Subsequences 解题报告(Python)

    [LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

  4. 【LeetCode】402. Remove K Digits 解题报告(Python)

    [LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  5. 【LeetCode】722. Remove Comments 解题报告(Python)

    [LeetCode]722. Remove Comments 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/remove-c ...

  6. 【LeetCode】19. Remove Nth Node From End of List 删除链表的倒数第 N 个结点

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:链表, 删除节点,双指针,题解,leetcode, 力扣 ...

  7. 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  8. 【leetcode】 26. Remove Duplicates from Sorted Array

    @requires_authorization @author johnsondu @create_time 2015.7.22 18:58 @url [remove dublicates from ...

  9. 【leetcode】1233. Remove Sub-Folders from the Filesystem

    题目如下: Given a list of folders, remove all sub-folders in those folders and return in any order the f ...

随机推荐

  1. 四:flask-URL两种传参方式(路径传参和get传参)

    新建一个视图 第一种:路径传参:url/参数:<参数名>,然后再视图函数中接收参数 也可以指定数据类型 string:默认使用此数据类型,接收没有任何斜杠"\/"的文本 ...

  2. Delphi Tokyo 10.2.3发布了

    Delphi Tokyo 10.2.3发布了 http://blog.sina.com.cn/s/blog_44fa172f0102wwwg.html (2018-03-14 07:51:32) 转载 ...

  3. FLUME安装&环境(一):netcat类型配置

    1.下载软件 在 /opt/deploy 下新建 flume 文件夹: # mkdir / opt/deploy / flume 到Flume官网上http://flume.apache.org/do ...

  4. HTML5——添加新元素 新元素 Canvas SVG MathML 黑客帝国特效

    为HTML添加新元素 添加新元素   +   该元素定义样式 <!DOCTYPE html> <html> <head> <meta charset=&quo ...

  5. win10安装Tensorflow1.9GPU版本

    前言 看到DateWhale出了一篇安装教程(微信公众号DateWhale),决定体验一下Tensorflow1.9的GPU版本..其实一开始装的是2.0,但是tf.Session()就报错了,说是2 ...

  6. MSSQL注入--反弹注入

    明明是sql注入的点,却无法进行注入,注射工具拆解的速度异常的缓慢,错误提示信息关闭,无法返回注入的结果,这个时候你便可以尝试使用反弹注入, 反弹注入需要依赖于函数opendatasource的支持, ...

  7. (转)Eclipse - CDT使用GDB调试C++的问题-无源文件命名(No source file named)

    http://tech.ddvip.com/2014-09/1411618782213496.html Eclipse CDT调试C++, 使用的Unix的调试器GDB; 由于在Unix下, 文件的目 ...

  8. Mac021--编辑软件

    一.思维导图MindNode 知乎Mac常用的思维导图:https://zhuanlan.zhihu.com/p/37768277 MindNode下载地址:https://macblcom.ctfi ...

  9. 关于migration build failed的问题

    首先一定要执行dotnet restore 查看网站的依赖关系(有时候生成是不报错的但是restore会找不到文件路径) 检查执行命令的路径是否是正确的当前网站路径 build failed一定是生成 ...

  10. 20191112 Spring Boot官方文档学习(4.4)

    4.4.日志 Spring Boot使用Commons Logging进行所有内部日志记录,但是使底层日志实现打开状态.为Java Util Logging,Log4J2和Logback提供了默认配置 ...