Two Pointer Simulation | Java | C++
Intuition, approach, and complexity discussed in video solution in detail\nhttps://youtu.be/Yq9W_ryX9OA\n# Code\nC++\n\nclass Solution {\npublic:\n    ListNode*
# Intuition, approach, and complexity discussed in video solution in detail\nhttps://youtu.be/Yq9W_ryX9OA\n# Code\nC++\n```\nclass Solution {\npublic:\n    ListNode* mergeNodes(ListNode* head) {\n        ListNode* prevZero = head;\n        ListNode* currNode = head;\n        while (currNode != nullptr) {\n            if (currNode->val != 0) {\n                int sum = currNode->val;\n                currNode = currNode->next;\n                while (currNode->val != 0) {\n                    sum += currNode->val;\n                    currNode = currNode->next;\n                }\n                prevZero->next->val = sum;\n                prevZero->next->next = currNode;\n                prevZero = currNode;\n            }\n            currNode = currNode->next;\n        }\n\n        ListNode dummyHead;\n        dummyHead.next = head;\n        currNode = &dummyHead;\n        while (currNode != nullptr && currNode->next != nullptr) {\n            if (currNode->next->val == 0) {\n                currNode->next = currNode->next->next;\n            } else {\n                currNode = currNode->next;\n            }\n        }\n        return dummyHead.next;\n    }\n};\n```\nJava\n```\nclass Solution {\n    public ListNode mergeNodes(ListNode head) {\n        ListNode prevZero = head;\n        ListNode currNode = head;\n        while(currNode != null){\n            if(currNode.val != 0){\n                int sum = currNode.val;\n                currNode = currNode.next;\n                while(currNode.val != 0){\n                    sum += currNode.val;\n                    currNode = currNode.next;\n                }\n                prevZero.next.val = sum;\n                prevZero.next.next = currNode;\n                prevZero = currNode;\n            }\n            currNode = currNode.next;\n        }\n        ListNode dummyHead = new ListNode();\n        dummyHead.next = head;\n        currNode = dummyHead;\n        while(currNode != null){\n            if(currNode.next.val == 0){\n                currNode.next = currNode.next.next;\n            }\n            currNode = currNode.next;\n        }\n        return dummyHead.next;\n    }\n}\n```
['Linked List', 'Simulation']