# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def removeZeroSumSublists(self, head: Optional[ListNode]) -> Optional[ListNode]:
runner = dummy = ListNode(next=head)
acc = 0
acc_to_node = dict()
while runner:
acc += runner.val
acc_to_node[acc] = runner
runner = runner.next
acc, runner = 0, dummy
while runner:
acc += runner.val
runner.next = acc_to_node[acc].next
runner = runner.next
return dummy.next
Python
복사