Skip to content

Commit a6fd1ac

Browse files
committed
203-remove-linked-list-elements.md Added Java, C++, JavaScript, C#, Ruby, Go solutions.
1 parent 92ff99c commit a6fd1ac

File tree

1 file changed

+167
-6
lines changed

1 file changed

+167
-6
lines changed

solutions/1-1000/203-remove-linked-list-elements.md

+167-6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ Assume that the node to be deleted in the linked list is `d`, and the previous n
3535

3636
To delete `d`, just set `p.next = p.next.next`.
3737

38+
Because `p.next.next` is used, the loop condition should be `while (p.next != null)` instead of `while (p != null)`.
39+
3840
But there is no node before the `head` node, which means that the `head` node needs to be treated specially.
3941

4042
Is there a way to make the `head` node no longer special? In this way, there is no need to treat the `head` specially.
@@ -47,7 +49,33 @@ The way is to introduce a `dummy` node, `dummy.next = head`.
4749

4850
## Java
4951
```java
50-
// Solution is from Coding5DotCom
52+
/**
53+
* Definition for singly-linked list.
54+
* public class ListNode {
55+
* int val;
56+
* ListNode next;
57+
* ListNode() {}
58+
* ListNode(int val) { this.val = val; }
59+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
60+
* }
61+
*/
62+
class Solution {
63+
public ListNode removeElements(ListNode head, int val) {
64+
var dummyHead = new ListNode();
65+
dummyHead.next = head;
66+
var node = dummyHead;
67+
68+
while (node.next != null) {
69+
if (node.next.val == val) {
70+
node.next = node.next.next;
71+
} else {
72+
node = node.next;
73+
}
74+
}
75+
76+
return dummyHead.next;
77+
}
78+
}
5179
```
5280

5381
## Python
@@ -75,27 +103,148 @@ class Solution:
75103

76104
## C++
77105
```cpp
78-
// Welcome to create a PR to complete the code of this language, thanks!
106+
/**
107+
* Definition for singly-linked list.
108+
* struct ListNode {
109+
* int val;
110+
* ListNode *next;
111+
* ListNode() : val(0), next(nullptr) {}
112+
* ListNode(int x) : val(x), next(nullptr) {}
113+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
114+
* };
115+
*/
116+
class Solution {
117+
public:
118+
ListNode* removeElements(ListNode* head, int val) {
119+
auto dummyHead = new ListNode();
120+
dummyHead->next = head;
121+
auto node = dummyHead;
122+
123+
while (node->next != nullptr) {
124+
if (node->next->val == val) {
125+
auto next_node = node->next;
126+
node->next = node->next->next;
127+
delete next_node;
128+
} else {
129+
node = node->next;
130+
}
131+
}
132+
133+
return dummyHead->next;
134+
}
135+
};
79136
```
80137

81138
## JavaScript
82139
```javascript
83-
// Welcome to create a PR to complete the code of this language, thanks!
140+
/**
141+
* Definition for singly-linked list.
142+
* function ListNode(val, next) {
143+
* this.val = (val===undefined ? 0 : val)
144+
* this.next = (next===undefined ? null : next)
145+
* }
146+
*/
147+
var removeElements = function (head, val) {
148+
const dummyHead = new ListNode()
149+
dummyHead.next = head
150+
let node = dummyHead
151+
152+
while (node.next != null) {
153+
if (node.next.val == val) {
154+
node.next = node.next.next
155+
} else {
156+
node = node.next
157+
}
158+
}
159+
160+
return dummyHead.next
161+
};
84162
```
85163

86164
## C#
87165
```c#
88-
// Welcome to create a PR to complete the code of this language, thanks!
166+
/**
167+
* Definition for singly-linked list.
168+
* public class ListNode {
169+
* public int val;
170+
* public ListNode next;
171+
* public ListNode(int val=0, ListNode next=null) {
172+
* this.val = val;
173+
* this.next = next;
174+
* }
175+
* }
176+
*/
177+
public class Solution {
178+
public ListNode RemoveElements(ListNode head, int val) {
179+
var dummyHead = new ListNode();
180+
dummyHead.next = head;
181+
var node = dummyHead;
182+
183+
while (node.next != null) {
184+
if (node.next.val == val) {
185+
node.next = node.next.next;
186+
} else {
187+
node = node.next;
188+
}
189+
}
190+
191+
return dummyHead.next;
192+
}
193+
}
89194
```
90195

91196
## Go
92197
```go
93-
// Welcome to create a PR to complete the code of this language, thanks!
198+
/**
199+
* Definition for singly-linked list.
200+
* type ListNode struct {
201+
* Val int
202+
* Next *ListNode
203+
* }
204+
*/
205+
func removeElements(head *ListNode, val int) *ListNode {
206+
dummyHead := &ListNode{}
207+
dummyHead.Next = head
208+
node := dummyHead
209+
210+
for node.Next != nil {
211+
if node.Next.Val == val {
212+
node.Next = node.Next.Next
213+
} else {
214+
node = node.Next
215+
}
216+
}
217+
218+
return dummyHead.Next
219+
}
94220
```
95221

96222
## Ruby
97223
```ruby
98-
# Welcome to create a PR to complete the code of this language, thanks!
224+
# Definition for singly-linked list.
225+
# class ListNode
226+
# attr_accessor :val, :next
227+
# def initialize(val = 0, _next = nil)
228+
# @val = val
229+
# @next = _next
230+
# end
231+
# end
232+
233+
def remove_elements(head, val)
234+
dummy_head = ListNode.new
235+
dummy_head.next = head
236+
node = dummy_head
237+
238+
while !node.next.nil?
239+
if node.next.val == val
240+
node.next = node.next.next
241+
else
242+
node = node.next
243+
end
244+
end
245+
246+
dummy_head.next
247+
end
99248
```
100249

101250
## C
@@ -123,9 +272,21 @@ class Solution:
123272
// Welcome to create a PR to complete the code of this language, thanks!
124273
```
125274

275+
## 问题描述
276+
给你一个链表的头节点 `head` 和一个整数 `val` ,请你删除链表中所有满足 `Node.val == val` 的节点,并返回 **新的头节点**
277+
278+
### [Example 1]
279+
![](../../images/examples/203_1.jpg)
280+
281+
**Input**: `head = [1,2,6,3,4,5,6], val = 6`
282+
283+
**Output**: `[1,2,3,4,5]`
284+
126285
## 中文题解
127286
假设链表中待删除的节点是`d``d`的前一个节点是`p`,所以`p.next`就是`d`。 删除`d`,只需要把`p.next = p.next.next`
128287

288+
因为用到了`p.next.next`,所以循环条件应为`while (p.next != null)`,而不是`while (p != null)`
289+
129290
`head`节点前面没有节点,这就意味着需要对`head`节点进行特殊处理。
130291

131292
是否有方法能够让`head`节点的不再特殊呢?这样就不需要特殊处理`head`了。

0 commit comments

Comments
 (0)