✅easy | with explanation(No dummy)|🔥Beats 100% in java,c++,python
\n\n\n# Explanation\n# Initialization:\n\n- The head of the list is updated to skip the initial zero.\n- A start pointer is used to iterate through the list.\n#
![Screenshot (1515).png](https://assets.leetcode.com/users/images/57179f3b-4b1c-403d-b3e0-a139d348a406_1720055664.8696363.png)\n\n\n# Explanation\n# Initialization:\n\n- The head of the list is updated to skip the initial zero.\n- A start pointer is used to iterate through the list.\n# Merging Nodes:\n\n- For each segment between zero nodes, a nested loop calculates the sum of node values.\n- The end pointer helps in traversing these segments.\n- The calculated sum is assigned to the node at the start pointer.\n- The next pointer of the start node is updated to bypass the segment and point to the next zero node.\n# Iteration:\n\n- The start pointer moves to the next segment and repeats the process.\n\n# Example\nConsider the linked list:\n\n\n```input []\n0 -> 3 -> 1 -> 0 -> 4 -> 5 -> 2 -> 0\n```\n\nThe steps would be:\n\n- Skip the initial 0.\n- Sum nodes 3 + 1 = 4, and update the list to 4 -> 0 -> 4 -> 5 -> 2 -> 0.\n- Sum nodes 4 + 5 + 2 = 11, and update the list to 4 -> 11 -> 0.\nThe final merged list is:\n\n\n```output []\n4 -> 11\n```\n# code\n\n```java []\npublic class Solution {\n    public ListNode mergeNodes(ListNode head) {\n        head = head.next;\n        ListNode start = head;\n        while (start != null) {\n            ListNode end = start;   \n            int sum = 0;\n            while (end.val != 0) {\n                sum += end.val;\n                end = end.next;\n            }\n            start.val = sum;   \n            start.next = end.next;   \n            start = start.next;\n        }\n        return head;\n    }\n\n```\n```c++ []\nclass Solution {\npublic:\n   ListNode* mergeNodes(ListNode* head) {\n     head=head->next;\n     ListNode* start=head;\n     while(start){\n\t    ListNode* end= start;   \n        int sum=0;\n        while(end->val!=0) sum+= end->val , end=end->next;\n        start->val=sum;   \n        start->next=end->next;  \n        start=start->next;   \n\t }\n     return head;\n}\n};\n```\n```python []\nclass Solution:\n    def mergeNodes(self, head: ListNode) -> ListNode:\n        head = head.next\n        start = head\n        while start:\n            end = start  \n            sum = 0\n            while end.val != 0:\n                sum += end.val\n                end = end.next\n            start.val = sum  \n            start.next = end.next  \n            start = start.next \n        return head\n\n```\n![e6f4f26f-a7f5-465b-8ec1-aa665db072f9_1719537871.6475258.png](https://assets.leetcode.com/users/images/176b18e8-af1b-417e-a4fd-055e0631f3a3_1720055574.2607713.png)\n\n
['Python', 'C++', 'Java']