@@ -35,6 +35,8 @@ Assume that the node to be deleted in the linked list is `d`, and the previous n
35
35
36
36
To delete ` d ` , just set ` p.next = p.next.next ` .
37
37
38
+ Because ` p.next.next ` is used, the loop condition should be ` while (p.next != null) ` instead of ` while (p != null) ` .
39
+
38
40
But there is no node before the ` head ` node, which means that the ` head ` node needs to be treated specially.
39
41
40
42
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`.
47
49
48
50
## Java
49
51
``` 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
+ }
51
79
```
52
80
53
81
## Python
@@ -75,27 +103,148 @@ class Solution:
75
103
76
104
## C++
77
105
``` 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
+ };
79
136
```
80
137
81
138
## JavaScript
82
139
``` 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
+ };
84
162
```
85
163
86
164
## C#
87
165
``` 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
+ }
89
194
```
90
195
91
196
## Go
92
197
``` 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
+ }
94
220
```
95
221
96
222
## Ruby
97
223
``` 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
99
248
```
100
249
101
250
## C
@@ -123,9 +272,21 @@ class Solution:
123
272
// Welcome to create a PR to complete the code of this language, thanks!
124
273
```
125
274
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
+
126
285
## 中文题解
127
286
假设链表中待删除的节点是` d ` ,` d ` 的前一个节点是` p ` ,所以` p.next ` 就是` d ` 。 删除` d ` ,只需要把` p.next = p.next.next ` 。
128
287
288
+ 因为用到了` p.next.next ` ,所以循环条件应为` while (p.next != null) ` ,而不是` while (p != null) ` 。
289
+
129
290
但` head ` 节点前面没有节点,这就意味着需要对` head ` 节点进行特殊处理。
130
291
131
292
是否有方法能够让` head ` 节点的不再特殊呢?这样就不需要特殊处理` head ` 了。
0 commit comments