-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path1171-remove-zero-sum-consecutive-nodes-from-linked-list.js
More file actions
48 lines (44 loc) · 1.25 KB
/
1171-remove-zero-sum-consecutive-nodes-from-linked-list.js
File metadata and controls
48 lines (44 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* 1171. Remove Zero Sum Consecutive Nodes from Linked List
* https://leetcode.com/problems/remove-zero-sum-consecutive-nodes-from-linked-list/
* Difficulty: Medium
*
* Given the head of a linked list, we repeatedly delete consecutive sequences of nodes that sum to
* 0 until there are no such sequences.
*
* After doing so, return the head of the final linked list. You may return any such answer.
*
* (Note that in the examples below, all sequences are serializations of ListNode objects.)
*/
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var removeZeroSumSublists = function(head) {
const node = new ListNode(0, head);
const map = new Map();
let prefixSum = 0;
let current = node;
while (current) {
prefixSum += current.val;
map.set(prefixSum, current);
current = current.next;
}
prefixSum = 0;
current = node;
while (current) {
prefixSum += current.val;
if (map.has(prefixSum) && map.get(prefixSum) !== current) {
current.next = map.get(prefixSum).next;
}
current = current.next;
}
return node.next;
};