@@ -27,17 +27,22 @@ Given the `head` of a linked list and an integer `val`, remove all the nodes of
27
27
- ` 0 <= val <= 50 `
28
28
29
29
## 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 ` .
31
31
32
- To delete ` d ` , just set ` p.next = p.next.next ` .
32
+ To delete `d`, just set `p.next = p.next.next`.
33
33
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) ` .
35
35
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.
37
37
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.
39
39
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>
41
46
42
47
## Complexity
43
48
* Time: ` O(n) ` .
@@ -216,6 +221,7 @@ func removeElements(head *ListNode, val int) *ListNode {
216
221
```
217
222
218
223
## Ruby
224
+
219
225
``` ruby
220
226
# Definition for singly-linked list.
221
227
# class ListNode
0 commit comments