Java Solution - O(n) time and O(n) space.
\n\n\n\n\n\n\n/**\n * Definition for singly-linked list.\n * public class ListNode {\n *     int val;\n *     ListNode next;\n *     ListNode() {}\n *     ListN
\n\n![image](https://assets.leetcode.com/users/images/501ed990-12c9-4c5c-8a9d-ae3472035b2a_1645329769.3603394.png)\n\n\n```\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        \n        ListNode t = head;\n        List<Integer> list = new ArrayList<>();\n        int sum =0;\n        while(t != null){\n            if(t.val == 0){\n                if(sum != 0){\n                    list.add(sum);\n                    sum =0;\n                }\n            }\n            sum += t.val;\n            t=t.next;\n        }\n        \n       ListNode root = null;\n        for (int i = list.size()-1; i >= 0 ; i--)\n        root = insert(root, list.get(i));\n        return root;\n    }\n    \n    \n    public ListNode insert(ListNode root, int item)\n    {\n        ListNode temp = new ListNode();\n        temp.val = item;\n        temp.next = root;\n        root = temp;\n        return root;\n    }\n}\n```\n
['Linked List', 'Dynamic Programming', 'Greedy', 'Recursion', 'Java']