File tree 2 files changed +50
-1
lines changed
2 files changed +50
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,164 LeetCode solutions in JavaScript
1
+ # 1,165 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
912
912
1163|[ Last Substring in Lexicographical Order] ( ./solutions/1163-last-substring-in-lexicographical-order.js ) |Hard|
913
913
1169|[ Invalid Transactions] ( ./solutions/1169-invalid-transactions.js ) |Medium|
914
914
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|
915
916
1189|[ Maximum Number of Balloons] ( ./solutions/1189-maximum-number-of-balloons.js ) |Easy|
916
917
1200|[ Minimum Absolute Difference] ( ./solutions/1200-minimum-absolute-difference.js ) |Easy|
917
918
1206|[ Design Skiplist] ( ./solutions/1206-design-skiplist.js ) |Hard|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments