标签:node self += col 提交 用户 move handle return
# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def removeZeroSumSublists(self, head: ListNode) -> ListNode: r=[] while head: r.append(head.val) head=head.next #print(r) i=0 while i<len(r): sum=r[i] j=i+1 while j<len(r) and sum!=0: sum+=r[j] j+=1 #print(i,j,sum) if sum==0: r=r[:i]+r[j:] else: i+=1 head=ListNode(0) p=head for i in range(len(r)): p.next=ListNode(r[i]) p=p.next return head.next
执行用时为 48 ms 的范例 # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def removeZeroSumSublists(self, head: ListNode) -> ListNode: handle = ListNode(0) handle.next = cur = head prefix = 0 m = {0:handle} while cur: prefix += cur.val if prefix in m: m[prefix].next = cur.next else: m[prefix] = cur cur = cur.next return handle.next
这个我还不能看得太懂,不会做。。。。
——2019.10.24
leetcode——1171. 从链表中删去总和值为零的连续节点
标签:node self += col 提交 用户 move handle return
原文地址:https://www.cnblogs.com/taoyuxin/p/11731394.html