-
Notifications
You must be signed in to change notification settings - Fork 24
/
1171-remove-zero-sum-consecutive-nodes-from-linked-list.js
79 lines (66 loc) · 1.95 KB
/
1171-remove-zero-sum-consecutive-nodes-from-linked-list.js
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// 1171. Remove Zero Sum Consecutive Nodes from Linked List
// Medium 41%
// 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.)
// 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]
// Constraints:
// The given linked list will contain between 1 and 1000 nodes.
// Each node in the linked list has -1000 <= node.val <= 1000.
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
const ListNode = require('../structs/ListNode')
/**
* @param {ListNode} head
* @return {ListNode}
*/
const removeZeroSumSublists = function(head) {
const h = new ListNode()
h.next = head
let subHead = h, sum = 0
while (subHead) {
sum = 0
let cur = subHead.next
while (cur) {
sum += cur.val
if (sum === 0) {
subHead.next = cur.next
}
cur = cur.next
}
subHead = subHead.next
}
return h.next
}
;[
[1,2,-3,3,1], // 3->1
[1,2,-3,3,1,-1,2], // 3->2
[1,2,3,-3,4], // 1->2->4
[1,2,3,-3,-2], // 1
[-1,-2,3,4,1], // 4->1
].forEach((array) => {
console.log(removeZeroSumSublists(ListNode.from(array)).toString())
})
// Solution:
// 寻找包含第一个节点的连续和为 0 的子序列,将其删除后在寻找第二个子序列,
// 再从更新后的链表中寻找包含第二个节点的子序列,依次寻找完所有节点。
// TO(n*n)
// 使用其他数据结构,可以降低时间复杂度。
// Submission Result: Accepted