Problem of the day
It is guaranteed that there will be no two consecutive zeroes, and there will always be a zero at the beginning and end of the linked list.
The first line of input contains the elements of the singly linked list separated by a single space. The -1 indicates the end of the singly linked list and hence, would never be a list element.
The first and the only output line contains the integers present in the linked list after all the merging operations have been performed.
You do not need to print anything, it has already been taken care of. Just implement the given function.
3 <= N <= 10^5
0 <= VAL <= 10^3
Where 'VAL' represents the integers present in the list.
Time limit: 1 sec
0 1 2 3 0 4 5 0 6 0 -1
6 9 6 -1
The given linked list is:
0 -> 1 -> 2 -> 3 -> 0 -> 4 -> 5 -> 0 -> 6 -> 0 -> null
Then, the linked list is converted to:
6 -> 9 -> 6 -> null
Taking 0s as the start and end in reference to a sequence, we can see that there are 3 sequences. They are:
1. 1 -> 2 -> 3, which sum to 6
2. 4 -> 5, which sum to 9
3. 6, which sum to 6 only
0 1 2 4 8 16 0 -1
31 -1