{C++} 2 Approaches : Without Dummy Node & With Dummy Node, Detailed Explanation Beats 99.5% & 99.77%
~ Please upvote if you find it helpful\u2764\uFE0F\n\n## Approach-1 : Without Using Dummy Node \u2705\n\n- We know that the beginning and the end of the LL will
> #### *~ Please upvote if you find it helpful\u2764\uFE0F*\n\n## Approach-1 : Without Using Dummy Node \u2705\n\n- We know that the **beginning** and the **end** of the *`LL`* will always have `Node.val == 0` & there are **no** two consecutive nodes with `Node.val == 0`.\n\n- From this we can tell that there will always exist **atleast** 1 group of elements between **zeros**.\n\n- We create 2 pointers `start` & `temp` to the second node of the ***LL*** and start traversing the ***LL*** using `temp` while adding up the values in the nodes till we encounter a node with `Node.val == 0`.\n\n- After encountering a Node with `Node.val == 0`, we change the `Node.val` of  `start` to sum and change its next pointer to the next Node of `temp` and reset **sum** to `0`.\n\n- Then the our `start` node becomes `temp->next` as we need the value after the second zero which is now our first zero.\n\n- We repeat this till we reach the end of the ***LL*** and then return its `head`. \n\n\n\n<!-- Describe your approach to solving the problem. -->\n\n```\n// Approach - 1 -> Without Using dummy node \n\nclass Solution {\npublic:\n    ListNode* mergeNodes(ListNode* head) {\n        \n        head=head->next;\n        ListNode *start = head, *temp = head;\n        int sum = 0;\n\n        while(temp){\n            if(temp->val!=0) sum+=temp->val;\n\n            else{\n                start->val = sum;\n                start->next = temp->next;\n                start = temp->next;\n                sum = 0;\n            }\n            temp=temp->next;\n        }\n\n        return head;\n\n    }\n};\n```\n\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---\n\n## Approach-2 : Using Dummy Node \u2705\n\n- Create a new Node `ans` this will act as our **resultant**.\n\n- Create a pointer to the node next of `head` as `temp` and a pointer to `ans` as `res`.\n\n- Start traversing the ***LL*** and add the values in the nodes to **sum** till we encounter a node with `Node.val == 0`.\n\n- Create a `dummy` node and point the next of `res` to it, this marks the **beginning** of our ans node. \n\n- Reset **sum** to **zero** and return the next node of `ans` \n\n> We return `ans->next` since the first node of ans has `Node.val == 0`.\n\n<br>\n\n```cpp\n// Approach-2 -> Using Dummmy Node\n\nclass Solution {\npublic:\n    ListNode* mergeNodes(ListNode* head) {\n        \n        ListNode *ans = new ListNode();\n        ListNode *temp = head->next, *res = ans;\n        int sum = 0;\n\n        while(temp){\n            if(temp->val!=0){\n                sum+=temp->val;\n            }\n            else{\n                ListNode *dummy = new ListNode(sum);\n                res->next=dummy;\n                res=res->next;\n                sum=0;\n            }\n            temp=temp->next;\n        }\n\n        return ans->next;\n\n    }\n};\n```\n\n### Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n\n\n\n![image.png](https://assets.leetcode.com/users/images/b4e058c0-2460-40c5-b7f5-a48ccec43721_1720063346.9718692.png)\n\n\n
['Linked List', 'Two Pointers', 'Simulation', 'C++']