Solution for LeetCode#2181
\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \nThe idea is to traverse the linked list and sum the values between the zeroes. Wh
![image.png](https://assets.leetcode.com/users/images/ebdf4e9a-31d1-4d67-a472-cb1286d6aa09_1720062793.5951967.png)\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe idea is to traverse the linked list and sum the values between the zeroes. Whenever a zero is encountered, if it\'s not the first zero, create a new node with the accumulated sum and append it to the result list.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n1. Initialize a dummy node to start the result linked list and a pointer tmp to keep track of the end of the result list.\n2. Traverse the input linked list with a pointer curr.\n3. Use a variable sum to accumulate the sum of values between zeroes.\n4. Use a variable zeroCnt to count zeroes encountered.\n5. For each node in the list:\n\t- Add its value to sum.\n\t- If the node\'s value is zero and it\'s not the first zero, create a new node with the value sum and append it to the result list, then reset sum.\n\t- If the node\'s value is zero, increment the zero count.\n6. Return the next of the dummy node as the head of the result list.\n\n# Complexity\n- Time complexity: O(n)\nWe traverse the entire list once, making the time complexity linear with respect to the number of nodes in the list.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\nWe only use a constant amount of extra space for variables.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n/**\n * Definition for singly-linked list.\n * public class ListNode {\n *     int val;\n *     ListNode next;\n *     ListNode() {}\n *     ListNode(int val) { this.val = val; }\n *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n * }\n */\nclass Solution {\n    public ListNode mergeNodes(ListNode head) {                       \n        ListNode result = new ListNode(); // Dummy node to start the result list\n        ListNode tmp = result; // Pointer to the end of the result list\n        \n        ListNode curr = head; // Pointer to traverse the input list\n        int sum = 0;\n        int zeroCnt = 0; // Counter for zeroes\n        \n        while (curr != null) {\n            sum += curr.val; // Accumulate the sum of values\n            \n            if (curr.val == 0 && zeroCnt != 0) { // If current value is zero and not the first zero\n                tmp.next = new ListNode(sum, null); // Create a new node with the accumulated sum\n                tmp = tmp.next; // Move the pointer to the end of the result list\n                sum = 0; // Reset the sum\n            }\n            \n            if (curr.val == 0) { \n                zeroCnt++; // Increment the zero count\n            }\n            \n            curr = curr.next; // Move to the next node\n        }\n        \n        return result.next; // Return the next of the dummy node as the head of the result list\n    }\n}\n\n```
['Java']