题目如下:

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. Jedis源码浅析

    1.概述 Jedis是redis官网推荐的redis java client,代码维护在github https://github.com/xetorthio/jedis. 本质上Jedis帮我们封装 ...

  2. 激活Win Server 2008 R2 Datacenter

    直接手撸KMS命令 管理员打开cmd输入: slmgr /skms kms.03k.org 然后用这个Key: 74YFP-3QFB3-KQT8W-PMXWJ-7M648

  3. 2018.03.29 python-pandas transform/apply 的使用

    #一般化的groupby方法:apply df = pd.DataFrame({'data1':np.random.rand(5), 'data2':np.random.rand(5), 'key1' ...

  4. C# 导出Excel文件 所导出文件打开时提示“Excel文件格式与扩展名指定格式不一致”

    Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); ...

  5. iOS客户端使用教程

    使用须知 支持 ios9.0 以上系统,兼容 iphone.ipad.ipod 等设备. 电脑上用 PP 助手安装 Shadowrocket   Mac电脑上用PP助手安装Shadowrocket 下 ...

  6. Environment Modules简单使用

    Environment Modules简单使用 Environment Modules简介 Typically users initialize their environment when they ...

  7. vue--》如何使用wacth监听对象的属性变化?

    在开发过程中,我们经常需要监听watch监听一个对象的变化,但是如何来实现     监听对象中属性的变化呢? 先回顾一下如何监听整个对象的变化,使用watch就行了 export default { ...

  8. JAVA第四周总结与实验2

    实验二 Java简单类与对象 一. 实验目的 (1) 掌握类的定义,熟悉属性.构造函数.方法的作用,掌握用类作为类型声明变量和方法返回值: (2) 理解类和对象的区别,掌握构造函数的使用,熟悉通过对象 ...

  9. realloc ------ 扩大malloc得到的内存空间

    char* p = malloc(1024);char* q = realloc(p,2048); 现在的问题是我们应该如何处理指针 p. 刚开始按照我最直观的理解,如果就是直接将 p = NULL; ...

  10. [转帖]kafka基础知识点总结

    kafka基础知识点总结 https://blog.csdn.net/qq_25445087/article/details/80270790 需要学习. 1.kafka简介 kafka是由Apach ...