Beats 100% of users with C++ || Step by Step Explain || Two Pointer Approach || Fast Solution ||
Abhiraj Pratap Singh\n\n---\n# UPVOTE IT....\n\n---\n\n\n\n---\n\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \n- The code seems 
# Abhiraj Pratap Singh\n\n---\n# UPVOTE IT....\n\n---\n\n![Screenshot (47).png](https://assets.leetcode.com/users/images/2acb9e67-a804-4dd2-856c-26ca46e568b3_1704734418.1839752.png)\n\n---\n\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- The code seems to be attempting to merge consecutive nodes with a value of 0 in a linked list.\n\n---\n\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Initialize two pointers, ptr1 and ptr2, pointing to the head of the linked list.\n2. Traverse the linked list with ptr1:\n    - If ptr1->val is 0, update the value of ptr2->val to the accumulated value and move ptr2 to the next node.\n    - If ptr1->val is not 0, accumulate its value in the value variable.\n3. Set ptr2->next to NULL to terminate the merged list.\n4. Return the original head of the linked list.\n\n---\n\n\n# Complexity\n\n---\n\n\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n- **Time complexity: O(n)** where n is the number of nodes in the linked list.\n\n---\n\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n- **Space complexity: O(1)** as the algorithm uses a constant amount of extra space regardless of the input size.\n\n---\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    {\n        ListNode *ptr1=head->next;\n        ListNode *ptr2=head;\n        int value=0;\n        while(ptr1!=NULL)\n        {\n            if(ptr1->val==0)\n            {\n                ptr2->val=value;\n                if(ptr1->next!=NULL)\n                    ptr2=ptr2->next;\n                value=0;\n            }\n            else\n            {\n                value=value+ptr1->val;\n            }\n            ptr1=ptr1->next;\n        }\n        ptr2->next=NULL;\n        return head;\n    }\n};\n```\n\n---\n\n# IF you like the solution please UPVOTE it.......\n\n---\n\n![discord-discordgifemoji.gif](https://assets.leetcode.com/users/images/09c246af-1a4e-49cc-a1fa-342add90dbe7_1704735035.5664375.gif)\n\n---\n\n\n![fucking-die-redditor-stupid-cuck-funny-cat.gif](https://assets.leetcode.com/users/images/61c8987a-8f88-4bcf-80e8-062e90d2040e_1704735039.6667283.gif)\n\n---\n\n
['Linked List', 'Two Pointers', 'Simulation', 'C++']