Python | Beats 98% | O(N) Time Complexity | O(1) Space Complexity | Explained
\n\n# Approach\n1. Create two pointers on poiting to head and another pointing to head.next.\n2. Initialize the sum as 0\n3. loop till the end, keep on adding t
\n\n# Approach\n1. Create two pointers on poiting to head and another pointing to head.next.\n2. Initialize the sum as 0\n3. loop till the end, keep on adding the value of 2nd pointer to sum variable till you encounter value 0\n4. when you encounter 0 set the value of sum to the next of first pointer.\n5. Keep on doing this till you reach at last node, at last set the 1st pointer Next to None so you only get three values to your head ListNode.\n6. Since the node always starts with 0 so return head.next to only return the sum values except the first 0.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\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        dummy = curr = head\n        curr = curr.next\n        sum = 0\n        while curr:\n            if curr.val == 0:\n                dummy = dummy.next\n                dummy.val = sum\n                sum = 0\n            else:\n                sum += curr.val\n            curr = curr.next\n        dummy.next = None\n        return head.next\n```
['Linked List', 'Two Pointers', 'Python', 'Python3']