在所有 python3 提交中击败了

在所有 python3 提交中击败了

# 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
                
执行用时 :180 ms, 在所有 python3 提交中击败了32.63%的用户
内存消耗 :13.9 MB, 在所有 python3 提交中击败了100.00%的用户
 
执行用时为 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

 
12-31 02:09