From 2dad2a17a9196a90a06f40f4b25895d0f0987c74 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Mon, 9 Sep 2024 07:24:09 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.2181 --- .../README.md | 62 ++++++++++------- .../README_EN.md | 66 +++++++++++-------- .../Solution.c | 2 +- .../Solution.cpp | 6 +- .../Solution.py | 2 +- .../Solution.rs | 28 ++++---- .../Solution.ts | 18 ++--- 7 files changed, 106 insertions(+), 78 deletions(-) diff --git a/solution/2100-2199/2181.Merge Nodes in Between Zeros/README.md b/solution/2100-2199/2181.Merge Nodes in Between Zeros/README.md index 277cb10774aa..54bdd920df6a 100644 --- a/solution/2100-2199/2181.Merge Nodes in Between Zeros/README.md +++ b/solution/2100-2199/2181.Merge Nodes in Between Zeros/README.md @@ -69,7 +69,15 @@ tags: -### 方法一 +### 方法一:模拟 + +我们定义一个虚拟头节点 $\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$ 为链表的长度。 @@ -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) @@ -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; @@ -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; } @@ -241,20 +249,24 @@ function mergeNodes(head: ListNode | null): ListNode | null { // } // } impl Solution { - pub fn merge_nodes(mut head: Option>) -> Option> { - 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>) -> Option> { + 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 } } ``` diff --git a/solution/2100-2199/2181.Merge Nodes in Between Zeros/README_EN.md b/solution/2100-2199/2181.Merge Nodes in Between Zeros/README_EN.md index ed3b2fa61104..5e8376c8445e 100644 --- a/solution/2100-2199/2181.Merge Nodes in Between Zeros/README_EN.md +++ b/solution/2100-2199/2181.Merge Nodes in Between Zeros/README_EN.md @@ -31,7 +31,7 @@ tags:
 Input: head = [0,3,1,0,4,5,2,0]
 Output: [4,11]
-Explanation: 
+Explanation:
 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.
@@ -42,7 +42,7 @@ The above figure represents the given linked list. The modified list contains
 
 Input: head = [0,1,0,3,0,2,2,0]
 Output: [1,3,4]
-Explanation: 
+Explanation:
 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.
@@ -65,7 +65,15 @@ The above figure represents the given linked list. The modified list contains
 
 
 
-### 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.
 
 
 
@@ -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)
@@ -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;
@@ -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;
 }
@@ -237,20 +245,24 @@ function mergeNodes(head: ListNode | null): ListNode | null {
 //   }
 // }
 impl Solution {
-    pub fn merge_nodes(mut head: Option>) -> Option> {
-        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>) -> Option> {
+        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
     }
 }
 ```
diff --git a/solution/2100-2199/2181.Merge Nodes in Between Zeros/Solution.c b/solution/2100-2199/2181.Merge Nodes in Between Zeros/Solution.c
index 4a9888c3abd7..abb11d7b716a 100644
--- a/solution/2100-2199/2181.Merge Nodes in Between Zeros/Solution.c	
+++ b/solution/2100-2199/2181.Merge Nodes in Between Zeros/Solution.c	
@@ -22,4 +22,4 @@ struct ListNode* mergeNodes(struct ListNode* head) {
         head = head->next;
     }
     return dummy.next;
-}
\ No newline at end of file
+}
diff --git a/solution/2100-2199/2181.Merge Nodes in Between Zeros/Solution.cpp b/solution/2100-2199/2181.Merge Nodes in Between Zeros/Solution.cpp
index 9955f1f2339c..79bfae3a6998 100644
--- a/solution/2100-2199/2181.Merge Nodes in Between Zeros/Solution.cpp	
+++ b/solution/2100-2199/2181.Merge Nodes in Between Zeros/Solution.cpp	
@@ -15,9 +15,9 @@ 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;
@@ -25,4 +25,4 @@ class Solution {
         }
         return dummy->next;
     }
-};
\ No newline at end of file
+};
diff --git a/solution/2100-2199/2181.Merge Nodes in Between Zeros/Solution.py b/solution/2100-2199/2181.Merge Nodes in Between Zeros/Solution.py
index c9cf69d10756..4c27295e1c62 100644
--- a/solution/2100-2199/2181.Merge Nodes in Between Zeros/Solution.py	
+++ b/solution/2100-2199/2181.Merge Nodes in Between Zeros/Solution.py	
@@ -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)
diff --git a/solution/2100-2199/2181.Merge Nodes in Between Zeros/Solution.rs b/solution/2100-2199/2181.Merge Nodes in Between Zeros/Solution.rs
index 400d676dab6e..b95f9d905374 100644
--- a/solution/2100-2199/2181.Merge Nodes in Between Zeros/Solution.rs	
+++ b/solution/2100-2199/2181.Merge Nodes in Between Zeros/Solution.rs	
@@ -15,19 +15,23 @@
 //   }
 // }
 impl Solution {
-    pub fn merge_nodes(mut head: Option>) -> Option> {
-        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>) -> Option> {
+        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
     }
 }
diff --git a/solution/2100-2199/2181.Merge Nodes in Between Zeros/Solution.ts b/solution/2100-2199/2181.Merge Nodes in Between Zeros/Solution.ts
index 997f336f7ac0..431bb6b01e6f 100644
--- a/solution/2100-2199/2181.Merge Nodes in Between Zeros/Solution.ts	
+++ b/solution/2100-2199/2181.Merge Nodes in Between Zeros/Solution.ts	
@@ -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;
 }