|
1 |
| -#203. Remove Linked List Elements - Best Practices of LeetCode Solutions |
2 |
| -LeetCode link:[203. Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements), |
3 |
| -[203. 移除链表元素](https://leetcode.cn/problems/remove-linked-list-elements) |
| 1 | +#203. 移除链表元素 - 力扣题解最佳实践 |
| 2 | +力扣链接:[203. 移除链表元素](https://leetcode.cn/problems/remove-linked-list-elements), 难度:**简单**。 |
4 | 3 |
|
5 |
| -[中文题解](#中文题解) |
6 |
| - |
7 |
| -##LeetCode problem description |
8 |
| -Given the`head` of a linked list and an integer`val`, remove all the nodes of the linked list that has`Node.val == val`, and return_the new head_. |
| 4 | +##力扣“203. 移除链表元素”问题描述 |
| 5 | +给你一个链表的头节点`head` 和一个整数`val` ,请你删除链表中所有满足`Node.val == val` 的节点,并返回**新的头节点** 。 |
9 | 6 |
|
10 |
| -###[Example 1] |
| 7 | +###[示例 1] |
11 | 8 | 
|
12 | 9 |
|
13 |
| -**Input**:`head = [1,2,6,3,4,5,6], val = 6` |
| 10 | +**输入**:`head = [1,2,6,3,4,5,6], val = 6` |
14 | 11 |
|
15 |
| -**Output**:`[1,2,3,4,5]` |
| 12 | +**输出**:`[1,2,3,4,5]` |
16 | 13 |
|
17 |
| -###[Example 2] |
18 |
| -**Input**:`head = [], val = 1` |
| 14 | +###[示例 2] |
| 15 | +**输入**:`head = [], val = 1` |
19 | 16 |
|
20 |
| -**Output**:`[]` |
| 17 | +**输出**:`[]` |
21 | 18 |
|
22 |
| -###[Example 3] |
23 |
| -**Input**:`head = [7,7,7,7], val = 7` |
| 19 | +###[示例 3] |
| 20 | +**输入**:`head = [7,7,7,7], val = 7` |
24 | 21 |
|
25 |
| -**Output**:`[]` |
| 22 | +**输出**:`[]` |
26 | 23 |
|
27 |
| -###[Constraints] |
28 |
| --The number of nodes in the list is in the range`[0, 10000]`. |
| 24 | +###[约束] |
| 25 | +-列表中的节点数目在范围`[0, 10000]` 内 |
29 | 26 | -`1 <= Node.val <= 50`
|
30 | 27 | -`0 <= val <= 50`
|
31 | 28 |
|
32 |
| -##Intuition |
33 |
| -[中文题解](#中文题解) |
34 |
| - |
35 |
| -Assume that the node to be deleted in the linked list is`d`, and the previous node of`d` is`p`, so`p.next` is`d`. |
36 |
| - |
37 |
| -To delete`d`, just set`p.next = p.next.next`. |
| 29 | +##思路 |
| 30 | +假设链表中待删除的节点是`d`,`d`的前一个节点是`p`,所以`p.next`就是`d`。 删除`d`,只需要把`p.next = p.next.next`。 |
38 | 31 |
|
39 |
| -Because`p.next.next` is used, the loop condition should be`while (p.next != null)` instead of`while (p != null)`. |
| 32 | +因为用到了`p.next.next`,所以循环条件应为`while (p.next != null)`,而不是`while (p != null)`。 |
40 | 33 |
|
41 |
| -But there is no node before the`head` node, which means that the`head` node needs to be treated specially. |
| 34 | +但`head`节点前面没有节点,这就意味着需要对`head`节点进行特殊处理。 |
42 | 35 |
|
43 |
| -Is there a way to make the`head` node no longer special? In this way, there is no need to treat the`head` specially. |
| 36 | +是否有方法能够让`head`节点的不再特殊呢?这样就不需要特殊处理`head`了。 |
44 | 37 |
|
45 |
| -The way is to introduce a`dummy` node,`dummy.next = head`. |
| 38 | +办法是引入`dummy`节点,`dummy.next = head`。 |
46 | 39 |
|
47 |
| -##Complexity |
48 |
| -*Time:`O(n)`. |
49 |
| -*Space:`O(1)`. |
| 40 | +##复杂度 |
| 41 | +*时间:`O(N)`。 |
| 42 | +*空间:`O(1)`。 |
50 | 43 |
|
51 | 44 | ##Java
|
52 | 45 | ```java
|
|
272 | 265 | ```
|
273 | 266 | // Welcome to create a PR to complete the code of this language, thanks!
|
274 | 267 | ```
|
275 |
| - |
276 |
| -##问题描述 |
277 |
| -给你一个链表的头节点`head` 和一个整数`val` ,请你删除链表中所有满足`Node.val == val` 的节点,并返回**新的头节点** 。 |
278 |
| - |
279 |
| -###[Example 1] |
280 |
| - |
281 |
| - |
282 |
| -**Input**:`head = [1,2,6,3,4,5,6], val = 6` |
283 |
| - |
284 |
| -**Output**:`[1,2,3,4,5]` |
285 |
| - |
286 |
| -##中文题解 |
287 |
| -假设链表中待删除的节点是`d`,`d`的前一个节点是`p`,所以`p.next`就是`d`。 删除`d`,只需要把`p.next = p.next.next`。 |
288 |
| - |
289 |
| -因为用到了`p.next.next`,所以循环条件应为`while (p.next != null)`,而不是`while (p != null)`。 |
290 |
| - |
291 |
| -但`head`节点前面没有节点,这就意味着需要对`head`节点进行特殊处理。 |
292 |
| - |
293 |
| -是否有方法能够让`head`节点的不再特殊呢?这样就不需要特殊处理`head`了。 |
294 |
| - |
295 |
| -办法是引入`dummy`节点,`dummy.next = head`。 |