Skip to content

Commit 9f0f5bc

Browse files
committed
一刷1171
1 parent d7bf10d commit 9f0f5bc

10 files changed

+121
-39
lines changed

README.adoc

+8-8
Original file line numberDiff line numberDiff line change
@@ -8222,14 +8222,14 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
82228222
//|{doc_base_url}/1170-compare-strings-by-frequency-of-the-smallest-character.adoc[题解]
82238223
//|Easy
82248224
//|
8225-
//
8226-
//|{counter:codes}
8227-
//|{leetcode_base_url}/remove-zero-sum-consecutive-nodes-from-linked-list/[1171. Remove Zero Sum Consecutive Nodes from Linked List^]
8228-
//|{source_base_url}/_1171_RemoveZeroSumConsecutiveNodesFromLinkedList.java[Java]
8229-
//|{doc_base_url}/1171-remove-zero-sum-consecutive-nodes-from-linked-list.adoc[题解]
8230-
//|Medium
8231-
//|
8232-
//
8225+
8226+
|{counter:codes}
8227+
|{leetcode_base_url}/remove-zero-sum-consecutive-nodes-from-linked-list/[1171. Remove Zero Sum Consecutive Nodes from Linked List^]
8228+
|{source_base_url}/_1171_RemoveZeroSumConsecutiveNodesFromLinkedList.java[Java]
8229+
|{doc_base_url}/1171-remove-zero-sum-consecutive-nodes-from-linked-list.adoc[题解]
8230+
|Medium
8231+
|
8232+
82338233
//|{counter:codes}
82348234
//|{leetcode_base_url}/dinner-plate-stacks/[1172. Dinner Plate Stacks^]
82358235
//|{source_base_url}/_1172_DinnerPlateStacks.java[Java]

docs/0000-03-prefix-sum.adoc

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
. xref:1109-corporate-flight-bookings.adoc[1109. Corporate Flight Bookings]
4242
. xref:1124-longest-well-performing-interval.adoc[1124. Longest Well-Performing Interval]
4343
. xref:1140-stone-game-ii.adoc[1140. Stone Game II]
44+
. xref:1171-remove-zero-sum-consecutive-nodes-from-linked-list.adoc[1171. 从链表中删去总和值为零的连续节点]
4445
. xref:1177-can-make-palindrome-from-substring.adoc[1177. Can Make Palindrome from Substring]
4546
. xref:1208-get-equal-substrings-within-budget.adoc[1208. Get Equal Substrings Within Budget]
4647
. xref:1248-count-number-of-nice-subarrays.adoc[1248. Count Number of Nice Subarrays]
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,85 @@
11
[#1171-remove-zero-sum-consecutive-nodes-from-linked-list]
2-
= 1171. Remove Zero Sum Consecutive Nodes from Linked List
2+
= 1171. 从链表中删去总和值为零的连续节点
33

4-
{leetcode}/problems/remove-zero-sum-consecutive-nodes-from-linked-list/[LeetCode - Remove Zero Sum Consecutive Nodes from Linked List^]
4+
https://leetcode.cn/problems/remove-zero-sum-consecutive-nodes-from-linked-list/[LeetCode - 1171. 从链表中删去总和值为零的连续节点 ^]
55

6-
Given the `head` of a linked list, we repeatedly delete consecutive sequences of nodes that sum to `0` until there are no such sequences.
6+
给你一个链表的头节点 `head`,请你编写代码,反复删去链表中由 *总和* 值为 `0` 的连续节点组成的序列,直到不存在这样的序列为止。
77

8-
After doing so, return the head of the final linked list. You may return any such answer.
8+
删除完毕后,请你返回最终结果链表的头节点。
99

10-
11-
(Note that in the examples below, all sequences are serializations of `ListNode` objects.)
10+
你可以返回任何满足题目要求的答案。
1211

13-
*Example 1:*
12+
(注意,下面示例中的所有序列,都是对 `ListNode` 对象序列化的表示。)
1413

15-
[subs="verbatim,quotes,macros"]
16-
----
17-
*Input:* head = [1,2,-3,3,1]
18-
*Output:* [3,1]
19-
*Note:* The answer [1,2,1] would also be accepted.
20-
----
14+
*示例 1:*
2115

22-
*Example 2:*
16+
....
17+
输入:head = [1,2,-3,3,1]
18+
输出:[3,1]
19+
提示:答案 [1,2,1] 也是正确的。
20+
....
2321

24-
[subs="verbatim,quotes,macros"]
25-
----
26-
*Input:* head = [1,2,3,-3,4]
27-
*Output:* [1,2,4]
28-
----
22+
*示例 2:*
2923

30-
*Example 3:*
24+
....
25+
输入:head = [1,2,3,-3,4]
26+
输出:[1,2,4]
27+
....
3128

32-
[subs="verbatim,quotes,macros"]
33-
----
34-
*Input:* head = [1,2,3,-3,-2]
35-
*Output:* [1]
36-
----
29+
*示例 3:*
30+
31+
....
32+
输入:head = [1,2,3,-3,-2]
33+
输出:[1]
34+
....
35+
36+
37+
*提示:*
3738

38-
39-
*Constraints:*
39+
* 给你的链表中可能有 `1``1000` 个节点。
40+
* 对于链表中的每个节点,节点的值:`+-1000 <= node.val <= 1000+`.
4041
4142
42-
* The given linked list will contain between `1` and `1000` nodes.
43-
* Each node in the linked list has `-1000 <= node.val <= 1000`.
43+
== 思路分析
4444

45+
想到了前缀和,但是没想到可以利用中间和为 `0`,直接跳过这部分节点。
4546

47+
image::images/1171-10.jpg[{image_attr}]
48+
49+
image::images/1171-11.jpg[{image_attr}]
50+
51+
image::images/1171-12.jpg[{image_attr}]
52+
53+
image::images/1171-13.png[{image_attr}]
4654

4755

4856
[[src-1171]]
57+
[tabs]
58+
====
59+
一刷::
60+
+
61+
--
4962
[{java_src_attr}]
5063
----
5164
include::{sourcedir}/_1171_RemoveZeroSumConsecutiveNodesFromLinkedList.java[tag=answer]
5265
----
66+
--
67+
68+
// 二刷::
69+
// +
70+
// --
71+
// [{java_src_attr}]
72+
// ----
73+
// include::{sourcedir}/_1171_RemoveZeroSumConsecutiveNodesFromLinkedList_2.java[tag=answer]
74+
// ----
75+
// --
76+
====
77+
78+
79+
== 参考资料
80+
81+
. https://leetcode.cn/problems/remove-zero-sum-consecutive-nodes-from-linked-list/solutions/1142176/qian-zhui-he-dan-lian-biao-de-qian-zhui-kwr05/[1171. 从链表中删去总和值为零的连续节点 - 【前缀和】单链表的前缀和超详细解法^]
82+
. https://leetcode.cn/problems/remove-zero-sum-consecutive-nodes-from-linked-list/solutions/2305659/javapython3qian-zhui-he-ha-xi-biao-yi-ci-6nya/[1171. 从链表中删去总和值为零的连续节点 - 前缀和 + 哈希表(一次遍历)【图解】^]
83+
. https://leetcode.cn/problems/remove-zero-sum-consecutive-nodes-from-linked-list/solutions/2297308/cong-lian-biao-zhong-shan-qu-zong-he-zhi-h18o/[1171. 从链表中删去总和值为零的连续节点 - 官方题解^]
84+
5385

docs/images/1171-10.jpg

122 KB
Loading

docs/images/1171-11.jpg

156 KB
Loading

docs/images/1171-12.jpg

241 KB
Loading

docs/images/1171-13.png

235 KB
Loading

docs/index.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -2426,7 +2426,7 @@ include::1143-longest-common-subsequence.adoc[leveloffset=+1]
24262426

24272427
// include::1170-compare-strings-by-frequency-of-the-smallest-character.adoc[leveloffset=+1]
24282428

2429-
// include::1171-remove-zero-sum-consecutive-nodes-from-linked-list.adoc[leveloffset=+1]
2429+
include::1171-remove-zero-sum-consecutive-nodes-from-linked-list.adoc[leveloffset=+1]
24302430

24312431
// include::1172-dinner-plate-stacks.adoc[leveloffset=+1]
24322432

logbook/202503.adoc

+6-1
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,12 @@ endif::[]
591591
|{counter:codes2503}
592592
|{leetcode_base_url}/number-of-good-leaf-nodes-pairs/[1530. 好叶子节点对的数量^]
593593
|{doc_base_url}/1530-number-of-good-leaf-nodes-pairs.adoc[题解]
594-
|⭕️
594+
|⭕️ 还有瑕疵,还需要继续优化。
595+
596+
|{counter:codes}
597+
|{leetcode_base_url}/remove-zero-sum-consecutive-nodes-from-linked-list/[1171. 从链表中删去总和值为零的连续节点^]
598+
|{doc_base_url}/1171-remove-zero-sum-consecutive-nodes-from-linked-list.adoc[题解]
599+
|❌ 前缀和。想到了前缀和,但是没想到可以利用中间和为 `0`,直接跳过这部分节点。
595600

596601
|===
597602

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import com.diguage.algo.util.ListNode;
4+
import com.diguage.util.ListNodes;
5+
6+
import java.util.Arrays;
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
10+
public class _1171_RemoveZeroSumConsecutiveNodesFromLinkedList {
11+
// tag::answer[]
12+
13+
/**
14+
* @author D瓜哥 · https://www.diguage.com
15+
* @since 2025-05-03 23:25:31
16+
*/
17+
public ListNode removeZeroSumSublists(ListNode head) {
18+
ListNode dummy = new ListNode(0);
19+
dummy.next = head;
20+
Map<Integer, ListNode> map = new HashMap<>();
21+
ListNode curr = dummy;
22+
int sum = 0;
23+
while (curr != null) {
24+
sum += curr.val;
25+
map.put(sum, curr);
26+
curr = curr.next;
27+
}
28+
sum = 0;
29+
curr = dummy;
30+
while (curr != null) {
31+
sum += curr.val;
32+
// map 中必然存在 key=sum 的值
33+
curr.next = map.get(sum).next;
34+
curr = curr.next;
35+
}
36+
return dummy.next;
37+
}
38+
39+
// end::answer[]
40+
public static void main(String[] args) {
41+
new _1171_RemoveZeroSumConsecutiveNodesFromLinkedList()
42+
.removeZeroSumSublists(ListNodes.build(Arrays.asList(1, 2, -3, 3, 1)));
43+
}
44+
}

0 commit comments

Comments
 (0)