head. A stretch is one or more consecutive nodes. While the list contains a stretch that sums to 0, delete one according to this rule:
- among all zero-sum stretches, take the one that starts furthest to the left;
- if there are several, take the longest.
Once no zero-sum stretch is left, return the head of the resulting list (which may be empty).head=list_node([4, 2, -6, 5, 1]) → [5, 1]
The leftmost zero-sum stretch is 4, 2, -6. Removing it leaves [5, 1], which has no zero-sum stretches.
head=list_node([3, 1, -1, 2, -2, 7]) → [3, 7]
The leftmost zero-sum stretch starts at 1, and the longest one from there is 1, -1, 2, -2. What remains is [3, 7].
head=list_node([5, -5, 0]) → None
The whole list sums to 0 and is removed entirely, so the answer is empty.
head=list_node([4, 2, -6, 5, 1])
[5, 1]
Run checks the first 3 tests in your browser. Submit checks all 121 tests on the server, hidden ones included.