Skip to content

Commit 22a60cf

Browse files
committed
feat: add solutions to lc problem: No.1171. Remove Zero Sum Consecutive Nodes from Linked List
1 parent 45a2169 commit 22a60cf

File tree

4 files changed

+139
-37
lines changed

4 files changed

+139
-37
lines changed

solution/1100-1199/1171.Remove Zero Sum Consecutive Nodes from Linked List/README.md

+56-3
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,80 @@
4444
<li>对于链表中的每个节点,节点的值:<code>-1000 &lt;= node.val &lt;= 1000</code>.</li>
4545
</ul>
4646

47-
4847
## 解法
4948

5049
<!-- 这里可写通用的实现逻辑 -->
5150

51+
“前缀和 + 哈希表”实现。
52+
53+
若链表节点的两个前缀和相等,说明两个前缀和之间的连续节点序列的和为 0,那么可以消去这部分连续节点。
54+
55+
第一次遍历链表,用哈希表 `pre_sum_node` 记录前缀和以及对应的链表节点,同一前缀和 s,**后者的链表节点覆盖前者**
56+
57+
第二次遍历链表,若当前节点 cur 的前缀和 s 在 `pre_sum_node` 出现,说明 cur 与 pre_sum_node[s] 之间的所有节点和为 0,直接修改 cur 的指向,`cur.next = pre_sum_node[s].next`,就删去了这部分和为 0 的连续节点。
58+
5259
<!-- tabs:start -->
5360

5461
### **Python3**
5562

5663
<!-- 这里可写当前语言的特殊实现逻辑 -->
5764

5865
```python
59-
66+
# Definition for singly-linked list.
67+
# class ListNode:
68+
# def __init__(self, x):
69+
# self.val = x
70+
# self.next = None
71+
72+
class Solution:
73+
def removeZeroSumSublists(self, head: ListNode) -> ListNode:
74+
dummy = ListNode(0)
75+
dummy.next = head
76+
s, cur = 0, dummy
77+
pre_sum_node = {}
78+
while cur:
79+
s += cur.val
80+
pre_sum_node[s] = cur
81+
cur = cur.next
82+
s, cur = 0, dummy
83+
while cur:
84+
s += cur.val
85+
cur.next = pre_sum_node[s].next
86+
cur = cur.next
87+
return dummy.next
6088
```
6189

6290
### **Java**
6391

6492
<!-- 这里可写当前语言的特殊实现逻辑 -->
6593

6694
```java
67-
95+
/**
96+
* Definition for singly-linked list.
97+
* public class ListNode {
98+
* int val;
99+
* ListNode next;
100+
* ListNode(int x) { val = x; }
101+
* }
102+
*/
103+
class Solution {
104+
public ListNode removeZeroSumSublists(ListNode head) {
105+
ListNode dummy = new ListNode(0);
106+
dummy.next = head;
107+
Map<Integer, ListNode> preSumNode = new HashMap<>();
108+
int s = 0;
109+
for (ListNode cur = dummy; cur != null; cur = cur.next) {
110+
s += cur.val;
111+
preSumNode.put(s, cur);
112+
}
113+
s = 0;
114+
for (ListNode cur = dummy; cur != null; cur = cur.next) {
115+
s += cur.val;
116+
cur.next = preSumNode.get(s).next;
117+
}
118+
return dummy.next;
119+
}
120+
}
68121
```
69122

70123
### **...**

solution/1100-1199/1171.Remove Zero Sum Consecutive Nodes from Linked List/README_EN.md

+48-6
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@
66

77
<p>Given the <code>head</code> of a linked list, we repeatedly delete consecutive sequences of nodes that sum to <code>0</code> until there are no such sequences.</p>
88

9-
10-
119
<p>After doing so, return the head of the final linked list.&nbsp; You may return any such answer.</p>
1210

13-
1411
<p>&nbsp;</p>
1512
<p>(Note that in the examples below, all sequences are serializations of <code>ListNode</code> objects.)</p>
1613

@@ -44,21 +41,66 @@
4441
<li>Each node in the linked list has <code>-1000 &lt;= node.val &lt;= 1000</code>.</li>
4542
</ul>
4643

47-
4844
## Solutions
4945

5046
<!-- tabs:start -->
5147

5248
### **Python3**
5349

5450
```python
55-
51+
# Definition for singly-linked list.
52+
# class ListNode:
53+
# def __init__(self, x):
54+
# self.val = x
55+
# self.next = None
56+
57+
class Solution:
58+
def removeZeroSumSublists(self, head: ListNode) -> ListNode:
59+
dummy = ListNode(0)
60+
dummy.next = head
61+
s, cur = 0, dummy
62+
pre_sum_node = {}
63+
while cur:
64+
s += cur.val
65+
pre_sum_node[s] = cur
66+
cur = cur.next
67+
s, cur = 0, dummy
68+
while cur:
69+
s += cur.val
70+
cur.next = pre_sum_node[s].next
71+
cur = cur.next
72+
return dummy.next
5673
```
5774

5875
### **Java**
5976

6077
```java
61-
78+
/**
79+
* Definition for singly-linked list.
80+
* public class ListNode {
81+
* int val;
82+
* ListNode next;
83+
* ListNode(int x) { val = x; }
84+
* }
85+
*/
86+
class Solution {
87+
public ListNode removeZeroSumSublists(ListNode head) {
88+
ListNode dummy = new ListNode(0);
89+
dummy.next = head;
90+
Map<Integer, ListNode> preSumNode = new HashMap<>();
91+
int s = 0;
92+
for (ListNode cur = dummy; cur != null; cur = cur.next) {
93+
s += cur.val;
94+
preSumNode.put(s, cur);
95+
}
96+
s = 0;
97+
for (ListNode cur = dummy; cur != null; cur = cur.next) {
98+
s += cur.val;
99+
cur.next = preSumNode.get(s).next;
100+
}
101+
return dummy.next;
102+
}
103+
}
62104
```
63105

64106
### **...**

solution/1100-1199/1171.Remove Zero Sum Consecutive Nodes from Linked List/Solution.java

+13-28
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,19 @@
88
*/
99
class Solution {
1010
public ListNode removeZeroSumSublists(ListNode head) {
11-
Map<Integer, ListNode> map = new HashMap<>();
12-
boolean isZeroSum = true;
13-
14-
while (isZeroSum) {
15-
isZeroSum = false;
16-
int sum = 0;
17-
ListNode temp = head;
18-
19-
while (temp != null) {
20-
sum += temp.val;
21-
22-
if (sum == 0) {
23-
head = temp.next;
24-
map.clear();
25-
isZeroSum = true;
26-
break;
27-
} else if (map.containsKey(sum)) {
28-
map.get(sum).next = temp.next;
29-
map.clear();
30-
isZeroSum = true;
31-
break;
32-
}
33-
34-
map.put(sum, temp);
35-
temp = temp.next;
36-
}
11+
ListNode dummy = new ListNode(0);
12+
dummy.next = head;
13+
Map<Integer, ListNode> preSumNode = new HashMap<>();
14+
int s = 0;
15+
for (ListNode cur = dummy; cur != null; cur = cur.next) {
16+
s += cur.val;
17+
preSumNode.put(s, cur);
3718
}
38-
39-
return head;
19+
s = 0;
20+
for (ListNode cur = dummy; cur != null; cur = cur.next) {
21+
s += cur.val;
22+
cur.next = preSumNode.get(s).next;
23+
}
24+
return dummy.next;
4025
}
4126
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution:
8+
def removeZeroSumSublists(self, head: ListNode) -> ListNode:
9+
dummy = ListNode(0)
10+
dummy.next = head
11+
s, cur = 0, dummy
12+
pre_sum_node = {}
13+
while cur:
14+
s += cur.val
15+
pre_sum_node[s] = cur
16+
cur = cur.next
17+
s, cur = 0, dummy
18+
while cur:
19+
s += cur.val
20+
cur.next = pre_sum_node[s].next
21+
cur = cur.next
22+
return dummy.next

0 commit comments

Comments
 (0)