✅Easy and Clean Code (C++ / Java / JavaScript) Beats 100%✅✅✅
Intuition\n Describe your first thoughts on how to solve this problem. \nThe core intuition behind this solution is to recognize that the problem can be broken 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n*The core intuition behind this solution is to recognize that the problem can be broken down into manageable segments: summing the values between zero nodes and then creating a new linked list with these sums. By iterating through the linked list once and maintaining a running sum, we can efficiently compute the desired output.*\n# Approach\n<!-- Describe your approach to solving the problem. -->\n**1 . Initialize Pointers and Variables:**\n\n*Create pointers for the new list\'s head (newHead) and tail (newTail).\nInitialize a variable sum to store the sum of the nodes between zeroes.*\n\n**2 . Iterate Through the List:**\n\n*Skip the first zero node.\nTraverse the list starting from the node after the head (head.next).\nSum Between Zeroes:*\n\n**3 . For each node, add its value to sum.**\n*When a zero node is encountered:\nUse the addList helper function to add the sum to the new list.\nReset the sum to zero.*\n\n**4 . Helper Function (addList):**\n\nThis function takes the current head and tail of the new list and a value to be added.\nIf the new list is empty, initialize it with the new node.\nIf not, add the new node at the end and update the tail pointer.*\n\n**5 . Return the New List:**\n\n*After the loop ends, return the new head of the list which contains the sums of nodes between zeroes.*\n# Complexity\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(N) [for creating new Answer List]\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```C++ []\nclass Solution {\n    void addList(ListNode* &head, ListNode* &tail, int val) {\n        if(head == NULL) {\n            head = new ListNode(val);\n            tail= head;\n        }\n        else {\n            ListNode* newNode = new ListNode(val);\n            tail->next = newNode;\n            tail = newNode;\n        }\n\n        return;\n    }\npublic:\n    ListNode* mergeNodes(ListNode* head) {\n\n        int sum = 0;\n        ListNode* newHead = NULL;\n        ListNode* newTail = NULL;\n        ListNode* temp = head->next;\n        while(temp != nullptr) {\n            if(temp->val == 0) {\n                addList(newHead, newTail, sum);\n                sum = 0;\n            }\n            sum += temp->val;\n            temp = temp->next;\n        }\n\n        return newHead;\n    }\n};\n```\n```Java []\nclass Solution {\n    void addList(ListNode[] nodes, int val) {\n        if (nodes[0] == null) {\n            nodes[0] = new ListNode(val);\n            nodes[1] = nodes[0];\n        } else {\n            ListNode newNode = new ListNode(val);\n            nodes[1].next = newNode;\n            nodes[1] = newNode;\n        }\n    }\n\n    public ListNode mergeNodes(ListNode head) {\n        int sum = 0;\n        ListNode[] nodes = new ListNode[2]; // nodes[0] will be newHead, nodes[1] will be newTail\n        ListNode temp = head.next;\n\n        while (temp != null) {\n            if (temp.val == 0) {\n                addList(nodes, sum);\n                sum = 0;\n            } else {\n                sum += temp.val;\n            }\n            temp = temp.next;\n        }\n\n        return nodes[0];\n    }\n}\n```\n```javascript []\nvar mergeNodes = function(head) {\n    function addList(nodes, val) {\n        if (nodes[0] === null) {\n            nodes[0] = new ListNode(val);\n            nodes[1] = nodes[0];\n        } else {\n            let newNode = new ListNode(val);\n            nodes[1].next = newNode;\n            nodes[1] = newNode;\n        }\n    }\n\n    let sum = 0;\n    let nodes = [null, null]; // nodes[0] will be newHead, nodes[1] will be newTail\n    let temp = head.next;\n\n    while (temp !== null) {\n        if (temp.val === 0) {\n            addList(nodes, sum);\n            sum = 0;\n        } else {\n            sum += temp.val;\n        }\n        temp = temp.next;\n    }\n\n    return nodes[0];\n};\n```\n![upvote.jpeg](https://assets.leetcode.com/users/images/3017d64f-54c5-4ae8-9c1a-5d61ae5700b6_1720068384.486587.jpeg)\n
['Linked List', 'Simulation', 'C++', 'Java', 'JavaScript']