JavaScript Simple Solution, Beats 97% time complexity and 98% space complexity
Intuition\nThis approach effectively iterates through the linked list, maintaining a running sum of values between zeros. When a zero is encountered, the sum is
# Intuition\nThis approach effectively iterates through the linked list, maintaining a running sum of values between zeros. When a zero is encountered, the sum is assigned to the corresponding node in the merged list, and the process repeats.\n\n# Approach\n1. Initialization: The code starts by initializing two pointers: headPointer and cur. headPointer will be used to traverse the merged list, while cur will iterate through the original list. A total variable is initialized to keep track of the sum of node values between consecutive zeros.\n\n2. Traversal: The while loop iterates through the linked list, moving cur to the next node in each iteration.\n\n3. Zero Encounter: If \n is 0, the code calculates the sum of all values between the last zero and the current zero and stores it in \n. The if statement after this handles scenarios where the current node is the last node in the list. If it is, it sets the next node of headPointer to null. If it is not, it moves headPointer to the next node in the merged list. Finally, total is reset to 0.\n\n4. Non-Zero Encounter: If \n is not 0, the code adds the value to the total variable, effectively accumulating the sum of non-zero values between consecutive zeros.\n\n5. Return: Once the loop completes, the original head of the linked list is returned, now containing the merged nodes with the sums between consecutive zeros.\n\n# Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(1)\n\n# Code\n```\n/**\n * Definition for singly-linked list.\n * function ListNode(val, next) {\n *     this.val = (val===undefined ? 0 : val)\n *     this.next = (next===undefined ? null : next)\n * }\n */\n/**\n * @param {ListNode} head\n * @return {ListNode}\n */\nvar mergeNodes = function (head) {\n  if (!head) {\n    return null;\n  }\n\n  let headPointer = head;\n\n  let cur = head.next;\n  let total = 0;\n\n  while (cur) {\n    if (cur.val === 0) {\n      headPointer.val = total;\n\n      if (!cur.next) {\n        headPointer.next = null;\n      } else {\n        headPointer = headPointer.next;\n      }\n\n      total = 0;\n    } else {\n      total += cur.val;\n    }\n\n    cur = cur.next;\n  }\n\n  return head;\n};\n```
['JavaScript']