Merge Nodes Between Zeros
The list starts and ends with a 0, and no two zeros are next to each other. Every stretch of nodes between two neighboring zeros should become a single node holding the sum of that stretch. The zeros disappear. Return the head of the new list. The list is passed in as head.
Linked lists appear in the tests as the list of their values, like [1,2,3]. Each node is a ListNode with .val and .next; the class is already defined for you.
Example 1
head [0,3,1,0,4,5,2,0]
returns [4,11]
3+1 = 4 and 4+5+2 = 11.
Example 2
head [0,1,0,3,0,2,2,0]
returns [1,3,4]
1, 3 and 4.
Constraints
3 <= number of nodes <= 100- The first and last values are 0, and no two zeros are adjacent
- Every other value is between 1 and 100
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.