C++ Simulation O(N) time
See my latest update in repo LeetCode\n\n\n## Solution 1. \n\ncpp\n// OJ: https://leetcode.com/contest/weekly-contest-281/problems/merge-nodes-in-between-zeros/
See my latest update in repo [LeetCode](https://github.com/lzl124631x/LeetCode)\n\n\n## Solution 1. \n\n```cpp\n// OJ: https://leetcode.com/contest/weekly-contest-281/problems/merge-nodes-in-between-zeros/\n// Author: github.com/lzl124631x\n// Time: O(N)\n// Space: O(1) extra space excluding those taken by answer\nclass Solution {\npublic:\n    ListNode* mergeNodes(ListNode* head) {\n        ListNode dummy, *tail = &dummy;\n        while (head) {\n            if (head->val == 0) head = head->next; // skip leading `0`\n            if (!head) break;\n            int sum = 0;\n            while (head->val != 0) { // sum numbers before the next `0`\n                sum += head->val;\n                head = head->next;\n            }\n            tail->next = new ListNode(sum); // append `sum`\n            tail = tail->next;\n        }\n        return dummy.next;\n    }\n};\n```\n\n## Solution 2.\n\nIf we are asked to do it in-place.\n\n```cpp\n// OJ: https://leetcode.com/contest/weekly-contest-281/problems/merge-nodes-in-between-zeros/\n// Author: github.com/lzl124631x\n// Time: O(N)\n// Space: O(1) as it\'s done in-place\nclass Solution {\npublic:\n    ListNode* mergeNodes(ListNode* head) {\n        ListNode dummy, *tail = &dummy;\n        while (head) {\n            if (head->val == 0) head = head->next;\n            if (!head) break;\n            auto node = head;\n            head = head->next;\n            while (head->val != 0) {\n                node->val += head->val;\n                head = head->next;\n            }\n            tail->next = node;\n            tail = tail->next;\n            node->next = nullptr;\n        }\n        return dummy.next;\n    }\n};\n```\n\nIf you are asked to add the code to free node:\n\n```cpp\n// OJ: https://leetcode.com/contest/weekly-contest-281/problems/merge-nodes-in-between-zeros/\n// Author: github.com/lzl124631x\n// Time: O(N)\n// Space: O(1) as it\'s done in-place\nclass Solution {\npublic:\n    ListNode* mergeNodes(ListNode* head) {\n        ListNode dummy, *tail = &dummy;\n        while (head) {\n            if (head->val == 0) {\n                auto p = head;\n                head = head->next;\n                delete p;\n            }\n            if (!head) break;\n            auto node = head;\n            head = head->next;\n            while (head->val != 0) {\n                node->val += head->val;\n                auto p = head;\n                head = head->next;\n                delete p;\n            }\n            tail->next = node;\n            tail = tail->next;\n            node->next = nullptr;\n        }\n        return dummy.next;\n    }\n};\n```
[]