Skip to content

Commit 4574dce

Browse files
authored
feat: add solutions to lc problem: No.2181 (#3495)
1 parent 35d66aa commit 4574dce

File tree

7 files changed

+106
-78
lines changed

7 files changed

+106
-78
lines changed

solution/2100-2199/2181.Merge Nodes in Between Zeros/README.md

+37-25
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,15 @@ tags:
6969

7070
<!-- solution:start -->
7171

72-
### 方法一
72+
### 方法一:模拟
73+
74+
我们定义一个虚拟头节点 $\textit{dummy}$,以及一个指向当前节点的指针 $\textit{tail}$,一个变量 $\textit{s}$ 用来记录当前节点的值之和。
75+
76+
接下来,我们从链表的第二个节点开始遍历,如果当前节点的值不为 0,我们将其加到 $\textit{s}$ 上,否则我们将 $\textit{s}$ 加到 $\textit{tail}$ 的后面,并将 $\textit{s}$ 置为 0,更新 $\textit{tail}$ 为 $\textit{tail}$ 的下一个节点。
77+
78+
最后,我们返回 $\textit{dummy}$ 的下一个节点。
79+
80+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为链表的长度。
7381

7482
<!-- tabs:start -->
7583

@@ -87,7 +95,7 @@ class Solution:
8795
s = 0
8896
cur = head.next
8997
while cur:
90-
if cur.val != 0:
98+
if cur.val:
9199
s += cur.val
92100
else:
93101
tail.next = ListNode(s)
@@ -149,9 +157,9 @@ public:
149157
ListNode* tail = dummy;
150158
int s = 0;
151159
for (ListNode* cur = head->next; cur; cur = cur->next) {
152-
if (cur->val)
160+
if (cur->val) {
153161
s += cur->val;
154-
else {
162+
} else {
155163
tail->next = new ListNode(s);
156164
tail = tail->next;
157165
s = 0;
@@ -206,16 +214,16 @@ func mergeNodes(head *ListNode) *ListNode {
206214

207215
function mergeNodes(head: ListNode | null): ListNode | null {
208216
const dummy = new ListNode();
209-
let cur = dummy;
210-
let sum = 0;
211-
while (head) {
212-
if (head.val === 0 && sum !== 0) {
213-
cur.next = new ListNode(sum);
214-
cur = cur.next;
215-
sum = 0;
217+
let tail = dummy;
218+
let s = 0;
219+
for (let cur = head.next; cur; cur = cur.next) {
220+
if (cur.val) {
221+
s += cur.val;
222+
} else {
223+
tail.next = new ListNode(s);
224+
tail = tail.next;
225+
s = 0;
216226
}
217-
sum += head.val;
218-
head = head.next;
219227
}
220228
return dummy.next;
221229
}
@@ -241,20 +249,24 @@ function mergeNodes(head: ListNode | null): ListNode | null {
241249
// }
242250
// }
243251
impl Solution {
244-
pub fn merge_nodes(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
245-
let mut dummy = Box::new(ListNode::new(-1));
246-
let mut cur = &mut dummy;
247-
let mut sum = 0;
248-
while let Some(node) = head {
249-
if node.val == 0 && sum != 0 {
250-
cur.next = Some(Box::new(ListNode::new(sum)));
251-
cur = cur.as_mut().next.as_mut().unwrap();
252-
sum = 0;
252+
pub fn merge_nodes(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
253+
let mut dummy = Box::new(ListNode::new(0));
254+
let mut tail = &mut dummy;
255+
let mut s = 0;
256+
let mut cur = head.unwrap().next;
257+
258+
while let Some(mut node) = cur {
259+
if node.val != 0 {
260+
s += node.val;
261+
} else {
262+
tail.next = Some(Box::new(ListNode::new(s)));
263+
tail = tail.next.as_mut().unwrap();
264+
s = 0;
253265
}
254-
sum += node.val;
255-
head = node.next;
266+
cur = node.next.take();
256267
}
257-
dummy.next.take()
268+
269+
dummy.next
258270
}
259271
}
260272
```

solution/2100-2199/2181.Merge Nodes in Between Zeros/README_EN.md

+39-27
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ tags:
3131
<pre>
3232
<strong>Input:</strong> head = [0,3,1,0,4,5,2,0]
3333
<strong>Output:</strong> [4,11]
34-
<strong>Explanation:</strong>
34+
<strong>Explanation:</strong>
3535
The above figure represents the given linked list. The modified list contains
3636
- The sum of the nodes marked in green: 3 + 1 = 4.
3737
- The sum of the nodes marked in red: 4 + 5 + 2 = 11.
@@ -42,7 +42,7 @@ The above figure represents the given linked list. The modified list contains
4242
<pre>
4343
<strong>Input:</strong> head = [0,1,0,3,0,2,2,0]
4444
<strong>Output:</strong> [1,3,4]
45-
<strong>Explanation:</strong>
45+
<strong>Explanation:</strong>
4646
The above figure represents the given linked list. The modified list contains
4747
- The sum of the nodes marked in green: 1 = 1.
4848
- The sum of the nodes marked in red: 3 = 3.
@@ -65,7 +65,15 @@ The above figure represents the given linked list. The modified list contains
6565

6666
<!-- solution:start -->
6767

68-
### Solution 1
68+
### Solution 1: Simulation
69+
70+
We define a dummy head node $\textit{dummy}$, a pointer $\textit{tail}$ pointing to the current node, and a variable $\textit{s}$ to record the sum of the values of the current nodes.
71+
72+
Next, we traverse the linked list starting from the second node. If the value of the current node is not 0, we add it to $\textit{s}$. Otherwise, we add $\textit{s}$ to the node after $\textit{tail}$, set $\textit{s}$ to 0, and update $\textit{tail}$ to the next node.
73+
74+
Finally, we return the node next to $\textit{dummy}$.
75+
76+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the linked list.
6977

7078
<!-- tabs:start -->
7179

@@ -83,7 +91,7 @@ class Solution:
8391
s = 0
8492
cur = head.next
8593
while cur:
86-
if cur.val != 0:
94+
if cur.val:
8795
s += cur.val
8896
else:
8997
tail.next = ListNode(s)
@@ -145,9 +153,9 @@ public:
145153
ListNode* tail = dummy;
146154
int s = 0;
147155
for (ListNode* cur = head->next; cur; cur = cur->next) {
148-
if (cur->val)
156+
if (cur->val) {
149157
s += cur->val;
150-
else {
158+
} else {
151159
tail->next = new ListNode(s);
152160
tail = tail->next;
153161
s = 0;
@@ -202,16 +210,16 @@ func mergeNodes(head *ListNode) *ListNode {
202210

203211
function mergeNodes(head: ListNode | null): ListNode | null {
204212
const dummy = new ListNode();
205-
let cur = dummy;
206-
let sum = 0;
207-
while (head) {
208-
if (head.val === 0 && sum !== 0) {
209-
cur.next = new ListNode(sum);
210-
cur = cur.next;
211-
sum = 0;
213+
let tail = dummy;
214+
let s = 0;
215+
for (let cur = head.next; cur; cur = cur.next) {
216+
if (cur.val) {
217+
s += cur.val;
218+
} else {
219+
tail.next = new ListNode(s);
220+
tail = tail.next;
221+
s = 0;
212222
}
213-
sum += head.val;
214-
head = head.next;
215223
}
216224
return dummy.next;
217225
}
@@ -237,20 +245,24 @@ function mergeNodes(head: ListNode | null): ListNode | null {
237245
// }
238246
// }
239247
impl Solution {
240-
pub fn merge_nodes(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
241-
let mut dummy = Box::new(ListNode::new(-1));
242-
let mut cur = &mut dummy;
243-
let mut sum = 0;
244-
while let Some(node) = head {
245-
if node.val == 0 && sum != 0 {
246-
cur.next = Some(Box::new(ListNode::new(sum)));
247-
cur = cur.as_mut().next.as_mut().unwrap();
248-
sum = 0;
248+
pub fn merge_nodes(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
249+
let mut dummy = Box::new(ListNode::new(0));
250+
let mut tail = &mut dummy;
251+
let mut s = 0;
252+
let mut cur = head.unwrap().next;
253+
254+
while let Some(mut node) = cur {
255+
if node.val != 0 {
256+
s += node.val;
257+
} else {
258+
tail.next = Some(Box::new(ListNode::new(s)));
259+
tail = tail.next.as_mut().unwrap();
260+
s = 0;
249261
}
250-
sum += node.val;
251-
head = node.next;
262+
cur = node.next.take();
252263
}
253-
dummy.next.take()
264+
265+
dummy.next
254266
}
255267
}
256268
```

solution/2100-2199/2181.Merge Nodes in Between Zeros/Solution.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ struct ListNode* mergeNodes(struct ListNode* head) {
2222
head = head->next;
2323
}
2424
return dummy.next;
25-
}
25+
}

solution/2100-2199/2181.Merge Nodes in Between Zeros/Solution.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ class Solution {
1515
ListNode* tail = dummy;
1616
int s = 0;
1717
for (ListNode* cur = head->next; cur; cur = cur->next) {
18-
if (cur->val)
18+
if (cur->val) {
1919
s += cur->val;
20-
else {
20+
} else {
2121
tail->next = new ListNode(s);
2222
tail = tail->next;
2323
s = 0;
2424
}
2525
}
2626
return dummy->next;
2727
}
28-
};
28+
};

solution/2100-2199/2181.Merge Nodes in Between Zeros/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]:
99
s = 0
1010
cur = head.next
1111
while cur:
12-
if cur.val != 0:
12+
if cur.val:
1313
s += cur.val
1414
else:
1515
tail.next = ListNode(s)

solution/2100-2199/2181.Merge Nodes in Between Zeros/Solution.rs

+16-12
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,23 @@
1515
// }
1616
// }
1717
impl Solution {
18-
pub fn merge_nodes(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
19-
let mut dummy = Box::new(ListNode::new(-1));
20-
let mut cur = &mut dummy;
21-
let mut sum = 0;
22-
while let Some(node) = head {
23-
if node.val == 0 && sum != 0 {
24-
cur.next = Some(Box::new(ListNode::new(sum)));
25-
cur = cur.as_mut().next.as_mut().unwrap();
26-
sum = 0;
18+
pub fn merge_nodes(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
19+
let mut dummy = Box::new(ListNode::new(0));
20+
let mut tail = &mut dummy;
21+
let mut s = 0;
22+
let mut cur = head.unwrap().next;
23+
24+
while let Some(mut node) = cur {
25+
if node.val != 0 {
26+
s += node.val;
27+
} else {
28+
tail.next = Some(Box::new(ListNode::new(s)));
29+
tail = tail.next.as_mut().unwrap();
30+
s = 0;
2731
}
28-
sum += node.val;
29-
head = node.next;
32+
cur = node.next.take();
3033
}
31-
dummy.next.take()
34+
35+
dummy.next
3236
}
3337
}

solution/2100-2199/2181.Merge Nodes in Between Zeros/Solution.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@
1212

1313
function mergeNodes(head: ListNode | null): ListNode | null {
1414
const dummy = new ListNode();
15-
let cur = dummy;
16-
let sum = 0;
17-
while (head) {
18-
if (head.val === 0 && sum !== 0) {
19-
cur.next = new ListNode(sum);
20-
cur = cur.next;
21-
sum = 0;
15+
let tail = dummy;
16+
let s = 0;
17+
for (let cur = head.next; cur; cur = cur.next) {
18+
if (cur.val) {
19+
s += cur.val;
20+
} else {
21+
tail.next = new ListNode(s);
22+
tail = tail.next;
23+
s = 0;
2224
}
23-
sum += head.val;
24-
head = head.next;
2525
}
2626
return dummy.next;
2727
}

0 commit comments

Comments
 (0)