✅Detailed Explanation💯Simple & Easy to Understand🔥O(n) Time and O(1) Space🔥🔥
\nSubmission link\n\n# \uD83C\uDFAF Problem Explanation\nWe are given the head of a linked list containing integers separated by 0\'s, with the beginning and en
![image.png](https://assets.leetcode.com/users/images/8e37b6ec-4030-4d5b-96a3-c023b21a1c86_1720057188.9848504.png)\n[Submission link](https://leetcode.com/problems/merge-nodes-in-between-zeros/submissions/1308801888?envType=daily-question&envId=2024-07-04)\n\n# \uD83C\uDFAF Problem Explanation\nWe are given the head of a linked list containing integers separated by 0\'s, with the beginning and end of the list having a value of 0. The task is to merge the nodes between every two consecutive 0\'s into a single node whose value is the sum of all the merged nodes. The modified list should exclude all the 0\'s.\n\n## \uD83E\uDD14 Intuition\n- The idea is to traverse the linked list by keep sum of encountered nodes in current node until we encounter a 0.\n- Once we encounter a 0, we know that we have summed up the nodes between the previous 0 till now in current node, so remove 0 node and move current pointer to next node which will next resultant node.\n- Continue this process until the end of the list.\n\n## \uD83E\uDDE0 Approach\n1. **Initialization**:\n    - Initialize a pointer `curr` from the head\'s next node, as the head is always 0. to keep track of the current node and a sum variable to store the sum between two 0\'s.\n\n2. **Traverse the Linked List**:\n    - For each node, if the value is not 0, add it to the sum and drop next node.\n    - If the value is 0, delete next node and point current node to next node.\n\n3. **Return**:\n    - Since the first node`head` is always 0, we can return its next node.\n\n# \u2699\uFE0F Dry Run:\n##### Input: head = [0,1,2,0,4,0]\n| Interation | Lisked List| Resultant | Process |\n|:--:|:--:|:--:|:------:|\n| 0 |0 -> ***1*** -> 2 -> 0 -> 4 -> 0 | [0,**1**,2,0,4,0] |Start `curr` with  head.next|\n| 1 |0 -> ***1+2*** ~-> **2**~ -> 0 -> 4 -> 0 | [0,**3**,0,4,0] |when `curr.next` is not 0 add next.val to curr.val and delete next |\n| 2 |0 -> ***3*** ~-> **0**~ -> 4 -> 0 | [0,3,**4**,0] |when `curr.next` is 0 delete next`0` and change curr ptr to next node |\n| 3 |0 -> 3 -> ***4*** ~-> **0**~ -> null | [0,3,4] **-** |when `curr.next` is 0 delete next`0` and change curr ptr to next node |\n| - |0 -> 3 -> 4 -> **null** | [0,3,4] | `curr` is `null` so we exit loop |\n\nAnd given linked list `head` will be 0 -> 3 -> 4\nReturning `head.next` gives 3 -> 4.\n##### Output: [3,4]\n\n# \uD83D\uDCD2 Complexity\n- \u23F0 **Time complexity**: $$O(n)$$ - We traverse the list once.\n- \uD83E\uDDFA **Space complexity**: $$O(1)$$ - only extra pointer is used.\n\n# \uD83E\uDDD1\u200D\uD83D\uDCBB Code\n``` python []\nclass Solution:\n    curr = head.next\n\n        while curr:\n            # If next node is not 0\n            if curr.next.val != 0:\n                # Add next.val to curr.val\n                curr.val += curr.next.val\n                # Delete next node\n                curr.next = curr.next.next\n            else:\n                # Delete next node, i.e 0\n                curr.next = curr.next.next\n                # Change curr ptr to 0\'s next node\n                curr = curr.next\n\n        return head.next\n```\n``` java []\nclass Solution {\n    public ListNode mergeNodes(ListNode head) {\n        ListNode curr = head.next;\n\n        while (curr != null) {\n            // If next node is not 0\n            if (curr.next.val != 0) {\n                // Add next.val to curr.val\n                curr.val += curr.next.val;\n                // Delete next node\n                curr.next = curr.next.next;\n            } else {\n                // Delete next node, i.e 0\n                curr.next = curr.next.next;\n                // Change curr ptr to 0\'s next node\n                curr = curr.next;\n            }\n        }\n\n        return head.next;\n    }\n}\n```\n``` cpp []\nclass Solution {\npublic:\n    ListNode* mergeNodes(ListNode* head) {\n        ListNode* curr = head->next;\n\n        while (curr) {\n            // If next node is not 0\n            if (curr->next->val != 0) {\n                // Add next.val to curr.val\n                curr->val += curr->next->val;\n                // Delete next node\n                curr->next = curr->next->next;\n            } else {\n                // Delete next node, i.e 0\n                curr->next = curr->next->next;\n                // Change curr ptr to 0\'s next node\n                curr = curr->next;\n            }\n        }\n\n        return head->next;\n    }\n};\n```\n``` javascript []\nvar mergeNodes = function(head) {\n    let curr = head.next;\n    while (curr !== null) {\n        // If next node is not 0\n        if (curr.next.val !== 0) {\n            // Add next.val to curr.val\n            curr.val += curr.next.val;\n            // Delete next node\n            curr.next = curr.next.next;\n        } else {\n            // Delete next node, i.e 0\n            curr.next = curr.next.next;\n            // Change curr ptr to 0\'s next node\n            curr = curr.next;\n        }\n    }\n    return head.next;\n}\n``` \n\n---\n\n\n# Please consider giving an Upvote!\n![upvote.png](https://assets.leetcode.com/users/images/5fbeecf2-18f6-4dce-a4a0-f11c859e46ad_1714711173.2665718.png)\n
['Linked List', 'Simulation', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']