Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.2181 #3495

Merged
merged 1 commit into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 37 additions & 25 deletions solution/2100-2199/2181.Merge Nodes in Between Zeros/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,15 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:模拟

我们定义一个虚拟头节点 $\textit{dummy}$,以及一个指向当前节点的指针 $\textit{tail}$,一个变量 $\textit{s}$ 用来记录当前节点的值之和。

接下来,我们从链表的第二个节点开始遍历,如果当前节点的值不为 0,我们将其加到 $\textit{s}$ 上,否则我们将 $\textit{s}$ 加到 $\textit{tail}$ 的后面,并将 $\textit{s}$ 置为 0,更新 $\textit{tail}$ 为 $\textit{tail}$ 的下一个节点。

最后,我们返回 $\textit{dummy}$ 的下一个节点。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为链表的长度。

<!-- tabs:start -->

Expand All @@ -87,7 +95,7 @@ class Solution:
s = 0
cur = head.next
while cur:
if cur.val != 0:
if cur.val:
s += cur.val
else:
tail.next = ListNode(s)
Expand Down Expand Up @@ -149,9 +157,9 @@ public:
ListNode* tail = dummy;
int s = 0;
for (ListNode* cur = head->next; cur; cur = cur->next) {
if (cur->val)
if (cur->val) {
s += cur->val;
else {
} else {
tail->next = new ListNode(s);
tail = tail->next;
s = 0;
Expand Down Expand Up @@ -206,16 +214,16 @@ func mergeNodes(head *ListNode) *ListNode {

function mergeNodes(head: ListNode | null): ListNode | null {
const dummy = new ListNode();
let cur = dummy;
let sum = 0;
while (head) {
if (head.val === 0 && sum !== 0) {
cur.next = new ListNode(sum);
cur = cur.next;
sum = 0;
let tail = dummy;
let s = 0;
for (let cur = head.next; cur; cur = cur.next) {
if (cur.val) {
s += cur.val;
} else {
tail.next = new ListNode(s);
tail = tail.next;
s = 0;
}
sum += head.val;
head = head.next;
}
return dummy.next;
}
Expand All @@ -241,20 +249,24 @@ function mergeNodes(head: ListNode | null): ListNode | null {
// }
// }
impl Solution {
pub fn merge_nodes(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut dummy = Box::new(ListNode::new(-1));
let mut cur = &mut dummy;
let mut sum = 0;
while let Some(node) = head {
if node.val == 0 && sum != 0 {
cur.next = Some(Box::new(ListNode::new(sum)));
cur = cur.as_mut().next.as_mut().unwrap();
sum = 0;
pub fn merge_nodes(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut dummy = Box::new(ListNode::new(0));
let mut tail = &mut dummy;
let mut s = 0;
let mut cur = head.unwrap().next;

while let Some(mut node) = cur {
if node.val != 0 {
s += node.val;
} else {
tail.next = Some(Box::new(ListNode::new(s)));
tail = tail.next.as_mut().unwrap();
s = 0;
}
sum += node.val;
head = node.next;
cur = node.next.take();
}
dummy.next.take()

dummy.next
}
}
```
Expand Down
66 changes: 39 additions & 27 deletions solution/2100-2199/2181.Merge Nodes in Between Zeros/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ tags:
<pre>
<strong>Input:</strong> head = [0,3,1,0,4,5,2,0]
<strong>Output:</strong> [4,11]
<strong>Explanation:</strong>
<strong>Explanation:</strong>
The above figure represents the given linked list. The modified list contains
- The sum of the nodes marked in green: 3 + 1 = 4.
- The sum of the nodes marked in red: 4 + 5 + 2 = 11.
Expand All @@ -42,7 +42,7 @@ The above figure represents the given linked list. The modified list contains
<pre>
<strong>Input:</strong> head = [0,1,0,3,0,2,2,0]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong>
<strong>Explanation:</strong>
The above figure represents the given linked list. The modified list contains
- The sum of the nodes marked in green: 1 = 1.
- The sum of the nodes marked in red: 3 = 3.
Expand All @@ -65,7 +65,15 @@ The above figure represents the given linked list. The modified list contains

<!-- solution:start -->

### Solution 1
### Solution 1: Simulation

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.

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.

Finally, we return the node next to $\textit{dummy}$.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the linked list.

<!-- tabs:start -->

Expand All @@ -83,7 +91,7 @@ class Solution:
s = 0
cur = head.next
while cur:
if cur.val != 0:
if cur.val:
s += cur.val
else:
tail.next = ListNode(s)
Expand Down Expand Up @@ -145,9 +153,9 @@ public:
ListNode* tail = dummy;
int s = 0;
for (ListNode* cur = head->next; cur; cur = cur->next) {
if (cur->val)
if (cur->val) {
s += cur->val;
else {
} else {
tail->next = new ListNode(s);
tail = tail->next;
s = 0;
Expand Down Expand Up @@ -202,16 +210,16 @@ func mergeNodes(head *ListNode) *ListNode {

function mergeNodes(head: ListNode | null): ListNode | null {
const dummy = new ListNode();
let cur = dummy;
let sum = 0;
while (head) {
if (head.val === 0 && sum !== 0) {
cur.next = new ListNode(sum);
cur = cur.next;
sum = 0;
let tail = dummy;
let s = 0;
for (let cur = head.next; cur; cur = cur.next) {
if (cur.val) {
s += cur.val;
} else {
tail.next = new ListNode(s);
tail = tail.next;
s = 0;
}
sum += head.val;
head = head.next;
}
return dummy.next;
}
Expand All @@ -237,20 +245,24 @@ function mergeNodes(head: ListNode | null): ListNode | null {
// }
// }
impl Solution {
pub fn merge_nodes(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut dummy = Box::new(ListNode::new(-1));
let mut cur = &mut dummy;
let mut sum = 0;
while let Some(node) = head {
if node.val == 0 && sum != 0 {
cur.next = Some(Box::new(ListNode::new(sum)));
cur = cur.as_mut().next.as_mut().unwrap();
sum = 0;
pub fn merge_nodes(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut dummy = Box::new(ListNode::new(0));
let mut tail = &mut dummy;
let mut s = 0;
let mut cur = head.unwrap().next;

while let Some(mut node) = cur {
if node.val != 0 {
s += node.val;
} else {
tail.next = Some(Box::new(ListNode::new(s)));
tail = tail.next.as_mut().unwrap();
s = 0;
}
sum += node.val;
head = node.next;
cur = node.next.take();
}
dummy.next.take()

dummy.next
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ struct ListNode* mergeNodes(struct ListNode* head) {
head = head->next;
}
return dummy.next;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ class Solution {
ListNode* tail = dummy;
int s = 0;
for (ListNode* cur = head->next; cur; cur = cur->next) {
if (cur->val)
if (cur->val) {
s += cur->val;
else {
} else {
tail->next = new ListNode(s);
tail = tail->next;
s = 0;
}
}
return dummy->next;
}
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]:
s = 0
cur = head.next
while cur:
if cur.val != 0:
if cur.val:
s += cur.val
else:
tail.next = ListNode(s)
Expand Down
28 changes: 16 additions & 12 deletions solution/2100-2199/2181.Merge Nodes in Between Zeros/Solution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@
// }
// }
impl Solution {
pub fn merge_nodes(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut dummy = Box::new(ListNode::new(-1));
let mut cur = &mut dummy;
let mut sum = 0;
while let Some(node) = head {
if node.val == 0 && sum != 0 {
cur.next = Some(Box::new(ListNode::new(sum)));
cur = cur.as_mut().next.as_mut().unwrap();
sum = 0;
pub fn merge_nodes(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut dummy = Box::new(ListNode::new(0));
let mut tail = &mut dummy;
let mut s = 0;
let mut cur = head.unwrap().next;

while let Some(mut node) = cur {
if node.val != 0 {
s += node.val;
} else {
tail.next = Some(Box::new(ListNode::new(s)));
tail = tail.next.as_mut().unwrap();
s = 0;
}
sum += node.val;
head = node.next;
cur = node.next.take();
}
dummy.next.take()

dummy.next
}
}
18 changes: 9 additions & 9 deletions solution/2100-2199/2181.Merge Nodes in Between Zeros/Solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@

function mergeNodes(head: ListNode | null): ListNode | null {
const dummy = new ListNode();
let cur = dummy;
let sum = 0;
while (head) {
if (head.val === 0 && sum !== 0) {
cur.next = new ListNode(sum);
cur = cur.next;
sum = 0;
let tail = dummy;
let s = 0;
for (let cur = head.next; cur; cur = cur.next) {
if (cur.val) {
s += cur.val;
} else {
tail.next = new ListNode(s);
tail = tail.next;
s = 0;
}
sum += head.val;
head = head.next;
}
return dummy.next;
}
Loading