-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy path1171. Remove Zero Sum Consecutive Nodes from Linked List.go
88 lines (83 loc) · 2.01 KB
/
1171. Remove Zero Sum Consecutive Nodes from Linked List.go
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
80
81
82
83
84
85
86
87
88
package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// ListNode define
type ListNode = structures.ListNode
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
// 解法一
func removeZeroSumSublists(head *ListNode) *ListNode {
// 计算累加和,和作为 key 存在 map 中,value 存那个节点的指针。如果字典中出现了重复的和,代表出现了和为 0 的段。
sum, sumMap, cur := 0, make(map[int]*ListNode), head
// 字典中增加 0 这个特殊值,是为了防止最终链表全部消除完
sumMap[0] = nil
for cur != nil {
sum = sum + cur.Val
if ptr, ok := sumMap[sum]; ok {
// 在字典中找到了重复的和,代表 [ptr, tmp] 中间的是和为 0 的段,要删除的就是这一段。
// 同时删除 map 中中间这一段的和
if ptr != nil {
iter := ptr.Next
tmpSum := sum + iter.Val
for tmpSum != sum {
// 删除中间为 0 的那一段,tmpSum 不断的累加删除 map 中的和
delete(sumMap, tmpSum)
iter = iter.Next
tmpSum = tmpSum + iter.Val
}
ptr.Next = cur.Next
} else {
head = cur.Next
sumMap = make(map[int]*ListNode)
sumMap[0] = nil
}
} else {
sumMap[sum] = cur
}
cur = cur.Next
}
return head
}
// 解法二 暴力解法
func removeZeroSumSublists1(head *ListNode) *ListNode {
if head == nil {
return nil
}
h, prefixSumMap, sum, counter, lastNode := head, map[int]int{}, 0, 0, &ListNode{Val: 1010}
for h != nil {
for h != nil {
sum += h.Val
counter++
if v, ok := prefixSumMap[sum]; ok {
lastNode, counter = h, v
break
}
if sum == 0 {
head = h.Next
break
}
prefixSumMap[sum] = counter
h = h.Next
}
if lastNode.Val != 1010 {
h = head
for counter > 1 {
counter--
h = h.Next
}
h.Next = lastNode.Next
}
if h == nil {
break
} else {
h, prefixSumMap, sum, counter, lastNode = head, map[int]int{}, 0, 0, &ListNode{Val: 1010}
}
}
return head
}