Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 545ae7b

Browse files
committed
1171 accepted. 4 ms, faster than 100.00% .
1 parent 3c9e0b0 commit 545ae7b

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

Algorithms/1171.remove-zero-sum-consecutive-nodes-from-linked-list/remove-zero-sum-consecutive-nodes-from-linked-list.go

+24-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,29 @@ import "github.com/aQuaYi/LeetCode-in-Go/kit"
66
type ListNode = kit.ListNode
77

88
func removeZeroSumSublists(head *ListNode) *ListNode {
9+
headPre := &ListNode{
10+
Val: 2000000, // bigger than 1000*1000
11+
Next: head,
12+
}
913

10-
return nil
14+
node := make(map[int]*ListNode, 1000)
15+
stack, top := make([]int, 1000), -1
16+
sum, cur := 0, headPre
17+
for cur != nil {
18+
sum += cur.Val
19+
if node[sum] == nil {
20+
node[sum] = cur
21+
top++
22+
stack[top] = sum
23+
} else {
24+
node[sum].Next = cur.Next
25+
for stack[top] != sum {
26+
node[stack[top]] = nil
27+
top--
28+
}
29+
}
30+
cur = cur.Next
31+
}
32+
33+
return headPre.Next
1134
}

Algorithms/1171.remove-zero-sum-consecutive-nodes-from-linked-list/remove-zero-sum-consecutive-nodes-from-linked-list_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func Test_removeZeroSumSublists(t *testing.T) {
6161

6262
for _, tc := range tcs {
6363
head := kit.Ints2List(tc.head)
64-
ans := removeZeroSumSublists(head)
64+
ans := kit.List2Ints(removeZeroSumSublists(head))
6565
a.Equal(tc.ans, ans, "输入:%v", tc)
6666
}
6767
}

0 commit comments

Comments
 (0)