Skip to content

Commit 8ade8ef

Browse files
committed
feat: add solutions to lc problem: No.0237
No.0237.Delete Node in a Linked List
1 parent 9a95f1d commit 8ade8ef

File tree

3 files changed

+176
-51
lines changed

3 files changed

+176
-51
lines changed

solution/0200-0299/0237.Delete Node in a Linked List/README.md

Lines changed: 77 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@
6363

6464
**方法一:节点赋值**
6565

66-
`node.next` 节点的值赋给 `node`,然后将 `node.next` 指向 `node.next` 的下一个节点。
66+
我们可以将当前节点的值替换为下一个节点的值,然后删除下一个节点。这样就可以达到删除当前节点的目的。
67+
68+
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
6769

6870
<!-- tabs:start -->
6971

@@ -110,23 +112,23 @@ class Solution {
110112
}
111113
```
112114

113-
### **JavaScript**
115+
### **C++**
114116

115-
```js
117+
```cpp
116118
/**
117119
* Definition for singly-linked list.
118-
* function ListNode(val) {
119-
* this.val = val;
120-
* this.next = null;
121-
* }
122-
*/
123-
/**
124-
* @param {ListNode} node
125-
* @return {void} Do not return anything, modify node in-place instead.
120+
* struct ListNode {
121+
* int val;
122+
* ListNode *next;
123+
* ListNode(int x) : val(x), next(NULL) {}
124+
* };
126125
*/
127-
var deleteNode = function (node) {
128-
node.val = node.next.val;
129-
node.next = node.next.next;
126+
class Solution {
127+
public:
128+
void deleteNode(ListNode* node) {
129+
node->val = node->next->val;
130+
node->next = node->next->next;
131+
}
130132
};
131133
```
132134
@@ -146,24 +148,73 @@ func deleteNode(node *ListNode) {
146148
}
147149
```
148150

149-
### **C++**
151+
### **TypeScript**
150152

151-
```cpp
153+
```ts
152154
/**
153155
* Definition for singly-linked list.
154-
* struct ListNode {
155-
* int val;
156-
* ListNode *next;
157-
* ListNode(int x) : val(x), next(NULL) {}
158-
* };
156+
* class ListNode {
157+
* val: number
158+
* next: ListNode | null
159+
* constructor(val?: number, next?: ListNode | null) {
160+
* this.val = (val===undefined ? 0 : val)
161+
* this.next = (next===undefined ? null : next)
162+
* }
163+
* }
159164
*/
160-
class Solution {
161-
public:
162-
void deleteNode(ListNode* node) {
163-
node->val = node->next->val;
164-
node->next = node->next->next;
165+
166+
/**
167+
Do not return anything, modify it in-place instead.
168+
*/
169+
function deleteNode(node: ListNode | null): void {
170+
node.val = node.next.val;
171+
node.next = node.next.next;
172+
}
173+
```
174+
175+
### **C#**
176+
177+
```cs
178+
/**
179+
* Definition for singly-linked list.
180+
* public class ListNode {
181+
* public int val;
182+
* public ListNode next;
183+
* public ListNode(int x) { val = x; }
184+
* }
185+
*/
186+
public class Solution {
187+
public void DeleteNode(ListNode node) {
188+
node.val = node.next.val;
189+
node.next = node.next.next;
165190
}
191+
}
192+
```
193+
194+
### **JavaScript**
195+
196+
```js
197+
/**
198+
* Definition for singly-linked list.
199+
* function ListNode(val) {
200+
* this.val = val;
201+
* this.next = null;
202+
* }
203+
*/
204+
/**
205+
* @param {ListNode} node
206+
* @return {void} Do not return anything, modify node in-place instead.
207+
*/
208+
var deleteNode = function (node) {
209+
node.val = node.next.val;
210+
node.next = node.next.next;
166211
};
167212
```
168213

214+
### **...**
215+
216+
```
217+
218+
```
219+
169220
<!-- tabs:end -->

solution/0200-0299/0237.Delete Node in a Linked List/README_EN.md

Lines changed: 80 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@
5656

5757
## Solutions
5858

59+
**Approach 1: Node assignment**
60+
61+
We can replace the value of the current node with the value of the next node, and then delete the next node. This can achieve the purpose of deleting the current node.
62+
63+
Time complexity $O(1)$, space complexity $O(1)$.
64+
5965
<!-- tabs:start -->
6066

6167
### **Python3**
@@ -97,23 +103,23 @@ class Solution {
97103
}
98104
```
99105

100-
### **JavaScript**
106+
### **C++**
101107

102-
```js
108+
```cpp
103109
/**
104110
* Definition for singly-linked list.
105-
* function ListNode(val) {
106-
* this.val = val;
107-
* this.next = null;
108-
* }
109-
*/
110-
/**
111-
* @param {ListNode} node
112-
* @return {void} Do not return anything, modify node in-place instead.
111+
* struct ListNode {
112+
* int val;
113+
* ListNode *next;
114+
* ListNode(int x) : val(x), next(NULL) {}
115+
* };
113116
*/
114-
var deleteNode = function (node) {
115-
node.val = node.next.val;
116-
node.next = node.next.next;
117+
class Solution {
118+
public:
119+
void deleteNode(ListNode* node) {
120+
node->val = node->next->val;
121+
node->next = node->next->next;
122+
}
117123
};
118124
```
119125
@@ -133,24 +139,73 @@ func deleteNode(node *ListNode) {
133139
}
134140
```
135141

136-
### **C++**
142+
### **TypeScript**
137143

138-
```cpp
144+
```ts
139145
/**
140146
* Definition for singly-linked list.
141-
* struct ListNode {
142-
* int val;
143-
* ListNode *next;
144-
* ListNode(int x) : val(x), next(NULL) {}
145-
* };
147+
* class ListNode {
148+
* val: number
149+
* next: ListNode | null
150+
* constructor(val?: number, next?: ListNode | null) {
151+
* this.val = (val===undefined ? 0 : val)
152+
* this.next = (next===undefined ? null : next)
153+
* }
154+
* }
146155
*/
147-
class Solution {
148-
public:
149-
void deleteNode(ListNode* node) {
150-
node->val = node->next->val;
151-
node->next = node->next->next;
156+
157+
/**
158+
Do not return anything, modify it in-place instead.
159+
*/
160+
function deleteNode(node: ListNode | null): void {
161+
node.val = node.next.val;
162+
node.next = node.next.next;
163+
}
164+
```
165+
166+
### **C#**
167+
168+
```cs
169+
/**
170+
* Definition for singly-linked list.
171+
* public class ListNode {
172+
* public int val;
173+
* public ListNode next;
174+
* public ListNode(int x) { val = x; }
175+
* }
176+
*/
177+
public class Solution {
178+
public void DeleteNode(ListNode node) {
179+
node.val = node.next.val;
180+
node.next = node.next.next;
152181
}
182+
}
183+
```
184+
185+
### **JavaScript**
186+
187+
```js
188+
/**
189+
* Definition for singly-linked list.
190+
* function ListNode(val) {
191+
* this.val = val;
192+
* this.next = null;
193+
* }
194+
*/
195+
/**
196+
* @param {ListNode} node
197+
* @return {void} Do not return anything, modify node in-place instead.
198+
*/
199+
var deleteNode = function (node) {
200+
node.val = node.next.val;
201+
node.next = node.next.next;
153202
};
154203
```
155204

205+
### **...**
206+
207+
```
208+
209+
```
210+
156211
<!-- tabs:end -->
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* val: number
5+
* next: ListNode | null
6+
* constructor(val?: number, next?: ListNode | null) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
* }
11+
*/
12+
13+
/**
14+
Do not return anything, modify it in-place instead.
15+
*/
16+
function deleteNode(node: ListNode | null): void {
17+
node.val = node.next.val;
18+
node.next = node.next.next;
19+
}

0 commit comments

Comments
 (0)