<p>Given the <code>head</code> of a linked list, we repeatedly delete consecutive sequences of nodes that sum to <code>0</code> until there are no such sequences.</p>

<p>After doing so, return the head of the final linked list.&nbsp; You may return any such answer.</p>

<p>&nbsp;</p>
<p>(Note that in the examples below, all sequences are serializations of <code>ListNode</code> objects.)</p>

<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> head = [1,2,-3,3,1]
<strong>Output:</strong> [3,1]
<strong>Note:</strong> The answer [1,2,1] would also be accepted.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> head = [1,2,3,-3,4]
<strong>Output:</strong> [1,2,4]
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> head = [1,2,3,-3,-2]
<strong>Output:</strong> [1]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The given linked list will contain between <code>1</code> and <code>1000</code> nodes.</li>
	<li>Each node in the linked list has <code>-1000 &lt;= node.val &lt;= 1000</code>.</li>
</ul>

# 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]:
        sums_ll = []
        cursor = head
        cursor_before = None

        while cursor is not None:
            if cursor.val == 0:
                if cursor_before is None:
                    head = cursor = cursor.next                    
                else:
                    cursor_before.next = cursor.next
                    cursor = cursor.next
                continue

            if len(sums_ll) > 0:
                # sum == 0을 찾는 부분. 직전 Node에서 계산한 sum list만 사용
                prev_sums = sums_ll[-1] 
                cur_sums = []
                zero_found = False
                for i in range(len(prev_sums)):
                    cur_sums.append(prev_sums[i] + cursor.val)
                    if cur_sums[i] == 0:
                        # i 부터 현재 노드까지를 삭제해야 함
                        if i == 0:
                            head = cursor.next
                            sums_ll = []
                            cursor_before = None
                            cursor = cursor.next
                        else:
                            ii = 0
                            cursor_from = head
                            while ii + 1 < i:
                                cursor_from = cursor_from.next
                                ii += 1
                            cursor_from.next = cursor.next
                            sums_ll = sums_ll[:i]
                            cursor_before = cursor_from
                            cursor = cursor.next
                        zero_found = True
                        break
                
                if not zero_found:
                    cur_sums.append(cursor.val)
                    sums_ll.append(cur_sums)
                    cursor_before = cursor
                    cursor = cursor.next
            else:
                cur_sums = [cursor.val]
                sums_ll.append(cur_sums)
                cursor_before = cursor
                cursor = cursor.next
            
        return head    
            
120