8 LoC | 1 POINTER | EASIEST of ALL GAURANTEED | INPLACE Value Addition | No other Variable
Intuition\n Describe your first thoughts on how to solve this problem. \nInplace addition of next values and elimination of next node after addition.\n# Approac
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nInplace addition of next values and elimination of next node after addition.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Initialize a pointer **cur** to the first node after the head.\n2. Iterate through the list:\n    - For each segment between two \'0\' nodes, sum the values and update the current node\'s value to this sum.\n    - Skip the nodes that have been summed by adjusting the **next** pointer of the current node.\n    - Once a \'0\' node is reached, move to the next segment by updating **cur**.\n3. Return the modified list starting from the node next to the head.\n# Complexity\n- Time complexity:\nThe time complexity is O(n), where n is the number of nodes in the linked list. Each node is visited once during the traversal.\n\n- Space complexity:\nThe space complexity is O(1), as the modifications are done in place without using any extra space apart from a few pointers.\n\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        cur = head.next\n        while cur:\n            # INPLACE ADDING OF NEXT VALUE UNTIL 0 FOUND\n            while cur.next.val != 0:\n                cur.val += cur.next.val\n                cur.next = cur.next.next\n            # MERGING AFTER ADDITION\n            cur.next = cur.next.next\n            cur = cur.next\n        return head.next\n```\nPLEASE UPVOTE !!!!!!!!!!!!!!!!!!!!!!!
['Python3']