Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 82558c4

Browse files
committedApr 5, 2025
Add solution #1171
1 parent 15c2c72 commit 82558c4

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,164 LeetCode solutions in JavaScript
1+
# 1,165 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -912,6 +912,7 @@
912912
1163|[Last Substring in Lexicographical Order](./solutions/1163-last-substring-in-lexicographical-order.js)|Hard|
913913
1169|[Invalid Transactions](./solutions/1169-invalid-transactions.js)|Medium|
914914
1170|[Compare Strings by Frequency of the Smallest Character](./solutions/1170-compare-strings-by-frequency-of-the-smallest-character.js)|Medium|
915+
1171|[Remove Zero Sum Consecutive Nodes from Linked List](./solutions/1171-remove-zero-sum-consecutive-nodes-from-linked-list.js)|Medium|
915916
1189|[Maximum Number of Balloons](./solutions/1189-maximum-number-of-balloons.js)|Easy|
916917
1200|[Minimum Absolute Difference](./solutions/1200-minimum-absolute-difference.js)|Easy|
917918
1206|[Design Skiplist](./solutions/1206-design-skiplist.js)|Hard|
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* 1171. Remove Zero Sum Consecutive Nodes from Linked List
3+
* https://leetcode.com/problems/remove-zero-sum-consecutive-nodes-from-linked-list/
4+
* Difficulty: Medium
5+
*
6+
* Given the head of a linked list, we repeatedly delete consecutive sequences of nodes that sum to
7+
* 0 until there are no such sequences.
8+
*
9+
* After doing so, return the head of the final linked list. You may return any such answer.
10+
*
11+
* (Note that in the examples below, all sequences are serializations of ListNode objects.)
12+
*/
13+
14+
/**
15+
* Definition for singly-linked list.
16+
* function ListNode(val, next) {
17+
* this.val = (val===undefined ? 0 : val)
18+
* this.next = (next===undefined ? null : next)
19+
* }
20+
*/
21+
/**
22+
* @param {ListNode} head
23+
* @return {ListNode}
24+
*/
25+
var removeZeroSumSublists = function(head) {
26+
const node = new ListNode(0, head);
27+
const map = new Map();
28+
let prefixSum = 0;
29+
30+
let current = node;
31+
while (current) {
32+
prefixSum += current.val;
33+
map.set(prefixSum, current);
34+
current = current.next;
35+
}
36+
37+
prefixSum = 0;
38+
current = node;
39+
while (current) {
40+
prefixSum += current.val;
41+
if (map.has(prefixSum) && map.get(prefixSum) !== current) {
42+
current.next = map.get(prefixSum).next;
43+
}
44+
current = current.next;
45+
}
46+
47+
return node.next;
48+
};

0 commit comments

Comments
 (0)
Please sign in to comment.