@@ -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
0 commit comments