【leetcode】1171. Remove Zero Sum Consecutive Nodes from Linked List
题目如下:
Given the
headof a linked list, we repeatedly delete consecutive sequences of nodes that sum to0until 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
ListNodeobjects.)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
1and1000nodes.- 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 ...
随机推荐
- 阶段2 JavaWeb+黑马旅游网_15-Maven基础_第1节 基本概念_03maven一键构建概念
资料里面写好的Helloworld项目 pom.xml存放jar包的坐标 首先复制项目所在的目录的路径: 先进去复制的这个路径,然后又输入了d盘.这时候就进去到这个项目目录了. 这个项目就运行起来了. ...
- 测开之路一百四十五:SQLAlchemy与后台模板整合之新增、查询、删除
实现在页面上点击展示页就展示内容,点击新增页就触发新增功能 项目结构 admin.__init__ from flask import Blueprint admin = Blueprint('adm ...
- Monkey测试:日志信息分析
在跑monkey时,我们需要将日志输出到文件,然后对日志信息进行分析. 一.输出日志到文件 在monkey命令后加>文件地址 如:adb shell monkey 1000>E:/text ...
- Python学习之==>操作Redis
一.redis简介 1.非关系型数据库 2.数据全部是存在内存里面 3.性能非常好,每秒支持30w次读写 4.可以通过备份数据库,把数据存到磁盘上来实现数据的持久化 二.操作redis 1.strin ...
- Excel公式的常见错误值及其解决方法
Excel公式的常见错误值及其解决方法 经常用Excel的朋友可能都会遇到一些莫名奇妙的错误值信息:# N/A!.#VALUE!.#DIV/O!等等,出现这些错误的原因有很多种,如果公式不能计算正确结 ...
- oracle 日期格式操作
原文转自:http://hi.baidu.com/gzfvb/blog/item/5062b7f008eb70a8a50f5206.html Oracle日期格式: to_date(" ...
- 手动设计神经网络进行MNIST分类
前言: 用手工设计的两层神经网络,经过200个epoch,最后得到0.9599,约0.96的精度 正文 import tensorflow as tf from tensorflow.examples ...
- jQuery 获取设置图片 src 的路径
1.获取: $('#img')[0].src 2.设置:$('#img').attr("src", data.CodeUrl);
- MySQL的count(*)性能怎么样?
对于count(主键id)来说,innodb引擎会遍历整张表,把每一行的id值都取出来,返回给server层,server层判断id值不为空,就按行累加 对于count(1)来说,innodb引擎遍历 ...
- [转帖]安全公告【安全公告】CVE-2019-0708远程桌面服务远程代码执行漏洞
[安全公告]CVE-2019-0708远程桌面服务远程代码执行漏洞 https://www.landui.com/help/nshow-9716.html 漏洞层出不穷 漏洞信息: 2019年5月14 ...