#1171
Remove Zero Sum Consecutive Nodes from Linked List
specialist · 825 · lc medium +31 · verified · 53.1% accepted · 3,503 likes · top 44%
Description
You are given the head of a linked list. Repeatedly find and remove any consecutive sequence of nodes whose values sum to 0, continuing until no such sequence remains.
Return the head of the resulting linked list. Any valid answer is accepted.
(Note: in the examples, all sequences are serializations of ListNode objects.)
Example 1:
Input: head = [1,2,-3,3,1]
Output: [3,1]
Note: The answer [1,2,1] would also be accepted.
Example 2:
Input: head = [1,2,3,-3,4]
Output: [1,2,4]
Example 3:
Input: head = [1,2,3,-3,-2]
Output: [1]
Code
1
2
3
4
5
6
7
8