Merge nodes in between zeros

September 6, 2022

linked-list

Problem URL: Merge nodes in between zeros

We will create a dummy node for our result. Then we will start from the head, sum the values of the nodes until we reach another 0, then attach a node to our result list. We will repeat it until we reach the end of the list.

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

class Solution:
    def mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]:
        if not head and not head.next:
            return head

        curSum = 0
        res = ListNode(-1)
        pointer = res
        curr = head.next

        while curr:
            if curr.val != 0:
                curSum += curr.val
            else:
                pointer.next = ListNode(curSum)
                curSum = 0
                pointer = pointer.next
            curr = curr.next

        return res.next

Time Complexity: O(n)
Space Complexity: O(1)