Skip to content

Commit 690427c

Browse files
committed
Updated 203-remove-linked-list-elements.md.
1 parent c1c5442 commit 690427c

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
temp/
33
en/empty-template-en.md
44
zh/empty-template-zh.md
5+
/.ruby-version

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,22 @@ Given the `head` of a linked list and an integer `val`, remove all the nodes of
2727
- `0 <= val <= 50`
2828

2929
## Intuition
30-
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`.
30+
- 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`.
3131

32-
To delete `d`, just set `p.next = p.next.next`.
32+
To delete `d`, just set `p.next = p.next.next`.
3333

34-
Because `p.next.next` is used, the loop condition should be `while (p.next != null)` instead of `while (p != null)`.
34+
- Because `p.next.next` is used, the loop condition should be `while (p.next != null)` instead of `while (p != null)`.
3535

36-
But there is no node before the `head` node, which means that the `head` node needs to be treated specially.
36+
- But there is no node before the `head` node, which means that the `head` node needs to be treated specially.
3737

38-
Is there a way to make the `head` node no longer special? In this way, there is no need to treat the `head` specially.
38+
Is there a way to make the `head` node no longer special? In this way, there is no need to treat the `head` specially.
3939

40-
The way is to introduce a `dummy` node, `dummy.next = head`.
40+
<details>
41+
<summary>
42+
Click to view the answer.
43+
</summary>
44+
<p>The way is to introduce a `dummy` node, `dummy.next = head`.</p>
45+
</details>
4146

4247
## Complexity
4348
* Time: `O(n)`.
@@ -216,6 +221,7 @@ func removeElements(head *ListNode, val int) *ListNode {
216221
```
217222

218223
## Ruby
224+
219225
```ruby
220226
# Definition for singly-linked list.
221227
# class ListNode

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,21 @@
2727
- `0 <= val <= 50`
2828

2929
## 思路
30-
假设链表中待删除的节点是`d``d`的前一个节点是`p`,所以`p.next`就是`d`。 删除`d`,只需要把`p.next = p.next.next`
3130

32-
因为用到了`p.next.next`,所以循环条件应为`while (p.next != null)`,而不是`while (p != null)`
31+
- 假设链表中待删除的节点是`d``d`的前一个节点是`p`,所以`p.next`就是`d`。 删除`d`,只需要把`p.next = p.next.next`
3332

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

36-
是否有方法能够让`head`节点的不再特殊呢?这样就不需要特殊处理`head`
35+
-`head`节点前面没有节点,这就意味着需要对`head`节点进行特殊处理
3736

38-
办法是引入`dummy`节点,`dummy.next = head`
37+
是否有方法能够让`head`节点的不再特殊呢?这样就不需要特殊处理`head`了。
38+
39+
<details>
40+
<summary>
41+
点击查看答案
42+
</summary>
43+
<p>办法是引入`dummy`节点,`dummy.next = head`。</p>
44+
</details>
3945

4046
## 复杂度
4147
* 时间:`O(N)`

0 commit comments

Comments
 (0)