Easy C++ Solution || Beats 90% || 2 pointer || No memory Leak (using delete)
\n\n# Approach\n Describe your approach to solving the problem. \nTwo pointer Approach with No memory Leak\n\n# Complexity\n- Time complexity: O(n)\n Add your t
![image.png](https://assets.leetcode.com/users/images/566ce2f3-2e07-4d17-9cdb-8b229456d28b_1720063724.3255036.png)\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nTwo pointer Approach with No memory Leak\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/**\n * Definition for singly-linked list.\n * struct ListNode {\n *     int val;\n *     ListNode *next;\n *     ListNode() : val(0), next(nullptr) {}\n *     ListNode(int x) : val(x), next(nullptr) {}\n *     ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n    ListNode* mergeNodes(ListNode* head) {\n        ListNode* ptr = head->next->next;\n        ListNode* start = head->next;\n        while (ptr) {\n            int sum = 0;\n            while (ptr && ptr->val != 0) {\n                sum += ptr->val;\n                start->next = ptr->next;\n                delete ptr;\n                ptr = start->next;\n            }\n            if (ptr) {\n                start->val += sum;\n                start->next = ptr->next;\n                delete ptr;\n                ptr = NULL;\n                start = start->next;\n                if (start)\n                    ptr = start->next;\n            }\n        }\n        head = head->next;\n        return head;\n    }\n};\n```
['Linked List', 'Simulation', 'C++']