aboutsummaryrefslogtreecommitdiff
path: root/merge_nodes_between_zero.py
blob: 7e730bd8884765d349dc139e4ec4a008536dbd7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 2181. Merge Nodes in Between Zeros

"""
given the 'head' of a linked list which contains a series of integers
separated by 0's. the beginning and end of the linked list will have
'node.val == 0'. for every consecutive 0's, merge all the nodes lying in
between them into a single node whose value is the sum of all the merged
nodes. the modified list should not contain any 0's. return the 'head' of the
modified linked list.
"""


# Definition for singly-linked list.
class ListNode(object):
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class Solution(object):
    def mergeNodes(self, head):
        """
        :type head: Optional[ListNode]
        :rtype: Optional[ListNode]
        """
        p1, p2 = head, head.next
        s = 0
        while p2:
            if p2.val == 0:
                p1 = p1.next
                p1.val = s
                s = 0
            else:
                s += p2.val
            p2 = p2.next
        p1.next = None
        return head.next