【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 to0
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
and1000
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的更多相关文章
- 【LeetCode】1171. Remove Zero Sum Consecutive Nodes from Linked List 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 preSum + 字典 日期 题目地址:https:/ ...
- leeetcode1171 Remove Zero Sum Consecutive Nodes from Linked List
""" Given the head of a linked list, we repeatedly delete consecutive sequences of no ...
- 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)
[LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- 【LeetCode】402. Remove K Digits 解题报告(Python)
[LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 【LeetCode】722. Remove Comments 解题报告(Python)
[LeetCode]722. Remove Comments 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/remove-c ...
- 【LeetCode】19. Remove Nth Node From End of List 删除链表的倒数第 N 个结点
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:链表, 删除节点,双指针,题解,leetcode, 力扣 ...
- 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 【leetcode】 26. Remove Duplicates from Sorted Array
@requires_authorization @author johnsondu @create_time 2015.7.22 18:58 @url [remove dublicates from ...
- 【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 ...
随机推荐
- 阶段3 1.Mybatis_11.Mybatis的缓存_1 今日课程安排
- 五:flask-url_for使用详解
from flask import url_for url_for(视图函数名):根据视图函数名指定url,只要视图函数不变,url随便变都不会影响 url_for源码: 示例视图,执行流程 带参数: ...
- Linux 命令 - man 查看命令的文档
man 命令是 Linux 中最常用的命令,碰到任何让你疑惑的命令,都可以 man 一下来查看详情.不只是 shell 命令,C 语言库函数和系统调用等内容也可以通过 man 命令查看. man 命令 ...
- vue路由高级用法
五.路由设置高级用法alias 别名 {path:'/list',component:MyList,alias:'/lists'}redirect 重定向 {path:'/productList',r ...
- numpy添加新的维度
原文链接:https://blog.csdn.net/xtingjie/article/details/72510834 numpy中包含的newaxis可以给原数组增加一个维度 np.newaxis ...
- 【Linux开发】【Qt开发】嵌入式Qt程序使用触屏或USB鼠标方式
上文<嵌入式Qt开发-移植到ARM开发板 >介绍了Qt程序的移植,本文再说下如何使开发板Qt程序使用触摸屏或USB方式进行交互. 之前刚把一个qt程序移植到arm板上成功运行显示时就开心的 ...
- Express中间件body-parser
在http请求种,POST.PUT.PATCH三种请求方法中包含着请求体,也就是所谓的request,在Nodejs原生的http模块中,请求体是要基于流的方式来接受和解析. body-parser是 ...
- 选择排序--python
def findSmallest(arr): smallest = arr[0] smallest_index = 0 for i in range(1, len(arr)): if arr[i] & ...
- Tomcat域名与服务器多对多配置
参考: https://www.cnblogs.com/yueshutong/p/9381566.html
- Python链表倒置的两种方法
实现链表的翻转: 思路一: def reverse(self): """ 翻转链表的第一种思路:依次改变结点的指向,将结点指向此结点的上一个结点,并使用pre来指向这个节 ...