Simple Java || C++ Code ☠️
java []\n/**\n * Definition for singly-linked list.\n * public class ListNode {\n *     int val;\n *     ListNode next;\n *     ListNode() {}\n *     ListNode(i
```java []\n/**\n * Definition for singly-linked list.\n * public class ListNode {\n *     int val;\n *     ListNode next;\n *     ListNode() {}\n *     ListNode(int val) { this.val = val; }\n *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n * }\n */\nclass Solution {\n    public ListNode mergeNodes(ListNode head) {\n        ListNode itr1 = head.next;\n        ListNode h = new ListNode();\n        ListNode itr2 = h;\n        int val=0;\n        while(itr1!=null){\n            if(itr1.val==0){\n                itr2.val=val;\n                if(itr1.next==null){\n                    break;\n                }\n                ListNode a = new ListNode();\n                itr2.next=a;\n                itr2=itr2.next;\n                val=0;\n            }\n            val+=itr1.val;\n            itr1=itr1.next;\n        }\n        return h;\n    }\n}\n```\n```C++ []\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* itr1 = head->next;\n        ListNode* h = new ListNode();\n        ListNode* itr2 = h;\n        int val=0;\n        while(itr1!=NULL){\n            if(itr1->val==0){\n                itr2->val=val;\n                if(itr1->next==NULL){\n                    break;\n                }\n                ListNode* a = new ListNode();\n                itr2->next=a;\n                itr2=itr2->next;\n                val=0;\n            }\n            val+=itr1->val;\n            itr1=itr1->next;\n        }\n        return h;\n    }\n};\n```\n\n\n\n\n### Explanation\n\nThe given code defines a method `mergeNodes` that processes a linked list to merge nodes between consecutive zeros into a single node. Here\'s a step-by-step breakdown:\n\n1. **Initialization:**\n   - `itr1` is set to `head.next` to skip the initial zero node.\n   - `h` is a new dummy node to form the result list.\n   - `itr2` is an iterator for the result list, initially pointing to `h`.\n   - `val` is used to accumulate the sum of nodes between zeros.\n\n2. **Iteration:**\n   - The code loops through the linked list using `itr1`.\n   - When a zero is encountered (`itr1.val == 0`):\n     - Assign the accumulated sum (`val`) to the current node in the result list (`itr2.val = val`).\n     - If the end of the list is reached (`itr1.next == null`), break the loop.\n     - Otherwise, create a new node, link it to the result list, and move `itr2` to this new node.\n     - Reset `val` to zero to start a new accumulation.\n   - Add the current node\'s value to `val` (`val += itr1.val`).\n   - Move `itr1` to the next node.\n\n3. **Return:**\n   - The method returns the head of the modified list (`h`).\n\n### Example\n\nConsider the input list: `0 -> 1 -> 2 -> 0 -> 3 -> 4 -> 0 -> 5 -> 0`\n\n1. Initialize:\n   - `itr1` points to `1`\n   - `h` points to `0`\n   - `itr2` points to `0`\n   - `val` is `0`\n\n2. Process:\n   - Add `1` to `val` (val = 1)\n   - Add `2` to `val` (val = 3)\n   - Encounter `0`, set `itr2.val` to `3` and create a new node for `itr2` to move.\n   - Add `3` to `val` (val = 3)\n   - Add `4` to `val` (val = 7)\n   - Encounter `0`, set `itr2.val` to `7` and create a new node for `itr2` to move.\n   - Add `5` to `val` (val = 5)\n   - Encounter `0`, set `itr2.val` to `5` and break the loop.\n\n3. Result:\n   - The modified list is: `3 -> 7 -> 5`\n\n### Complexity Analysis\n\n- **Time Complexity:** O(n)\n  - The code traverses the linked list once, performing constant-time operations for each node. Thus, the time complexity is linear in terms of the number of nodes (`n`).\n\n- **Space Complexity:** O(1)\n  - The code uses a few pointers and a variable for accumulation, resulting in constant extra space usage. Thus, the space complexity is constant.\n
['Linked List', 'Simulation', 'C++', 'Java']