Python + Recursion
Intuition\n Describe your first thoughts on how to solve this problem. \nThe solution will combine the first few nodes until the next 0 and then connect that no
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe solution will combine the first few nodes until the next 0 and then connect that node to the rest of the nodes merged. Therefore, we can use a recursive method to find the solution. \n# Approach\n<!-- Describe your approach to solving the problem. -->\nIf the value of the head is 0 and the next node is null, return null. Otherwise, merge nodes by adding the value of the next node until the next zero is reached. Then set head.next to the recursive call on the rest of the nodes.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(n)$$\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(1)$$\n# Code\n```\n# Definition for singly-linked list.\n# class ListNode:\n#     def __init__(self, val=0, next=None):\n#         self.val = val\n#         self.next = next\nclass Solution:\n    def mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]:\n        if head.val == 0 and head.next is None:\n            return None\n        \n        while head.next.val != 0:\n            head.val += head.next.val\n            head.next = head.next.next\n        \n        head.next = self.mergeNodes(head.next)\n\n        return head\n```
['Python3']