beats 100% users | Java | Simple Approach
\n\n# Approach\n Describe your approach to solving the problem. \n1. Initialize modify as head.next and nextSum as modify.\n2. Iterate through the list using a 
![image.png](https://assets.leetcode.com/users/images/6522b79a-79a5-45ef-a19e-07ab943abfcf_1720087323.728512.png)\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Initialize modify as head.next and nextSum as modify.\n2. Iterate through the list using a while loop with the condition nextSum != null.\n3. Within the loop, initialize a sum variable to 0.\n4. Use another while loop to calculate the sum of consecutive nodes by traversing the nextSum list until nextSum is not null.\n5. Update modify.val with the calculated sum.\n6. Move nextSum to the next node and modify to the next node as well.\n7. Return head.next after the loop completes to return the updated list.\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# Code\n```\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 modify=head.next;\n        ListNode nextSum=modify;\n\n        while(nextSum!=null){\n            int sum=0;\n            while(nextSum.val!=0){\n                sum+=nextSum.val;\n                nextSum=nextSum.next;\n            }\n             modify.val = sum;\n            nextSum = nextSum.next;\n            \n            modify.next = nextSum;\n            modify = modify.next;\n        }\n        return head.next;\n    }\n}\n```
['Java']