|
| 1 | +# 203. Remove Linked List Elements - LeetCode Solution |
| 2 | +LeetCode problem link: [203. Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements) |
| 3 | + |
| 4 | +## LeetCode problem description |
| 5 | +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_. |
| 6 | + |
| 7 | +### [Example 1] |
| 8 | + |
| 9 | +**Input**: `head = [1,2,6,3,4,5,6], val = 6` |
| 10 | + |
| 11 | +**Output**: `[1,2,3,4,5]` |
| 12 | + |
| 13 | +### [Example 2] |
| 14 | +**Input**: `head = [], val = 1` |
| 15 | + |
| 16 | +**Output**: `[]` |
| 17 | + |
| 18 | +### [Example 3] |
| 19 | +**Input**: `head = [7,7,7,7], val = 7` |
| 20 | + |
| 21 | +**Output**: `[]` |
| 22 | + |
| 23 | +### [Constraints] |
| 24 | +- The number of nodes in the list is in the range `[0, 10000]`. |
| 25 | +- `1 <= Node.val <= 50` |
| 26 | +- `0 <= val <= 50` |
| 27 | + |
| 28 | +## Intuition behind the Solution |
| 29 | +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 | + |
| 31 | +To delete `d`, just set `p.next = p.next.next`. |
| 32 | + |
| 33 | +But there is no node before the `head` node, which means that the `head` node needs to be treated specially. |
| 34 | +Is there a way to make the `head` node no longer special? In this way, there is no need to treat the `head` specially. |
| 35 | +The way is to introduce a `dummy` node, `dummy.next = head`. |
| 36 | + |
| 37 | +## Complexity |
| 38 | +* Time: `O(n)`. |
| 39 | +* Space: `O(1)`. |
| 40 | + |
| 41 | +## Java |
| 42 | +```java |
| 43 | +// Solution is from Coding5DotCom |
| 44 | +``` |
| 45 | + |
| 46 | +## Python |
| 47 | +```python |
| 48 | +# Definition for singly-linked list. |
| 49 | +# class ListNode: |
| 50 | +# def __init__(self, val=0, next=None): |
| 51 | +# self.val = val |
| 52 | +# self.next = next |
| 53 | + |
| 54 | +class Solution: |
| 55 | + def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]: |
| 56 | + dummy_head = ListNode() |
| 57 | + dummy_head.next = head |
| 58 | + node = dummy_head |
| 59 | + |
| 60 | + while node.next: |
| 61 | + if node.next.val == val: |
| 62 | + node.next = node.next.next |
| 63 | + else: |
| 64 | + node = node.next |
| 65 | + |
| 66 | + return dummy_head.next |
| 67 | +``` |
| 68 | + |
| 69 | +## C++ |
| 70 | +```cpp |
| 71 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 72 | +``` |
| 73 | + |
| 74 | +## JavaScript |
| 75 | +```javascript |
| 76 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 77 | +``` |
| 78 | + |
| 79 | +## C# |
| 80 | +```c# |
| 81 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 82 | +``` |
| 83 | + |
| 84 | +## Go |
| 85 | +```go |
| 86 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 87 | +``` |
| 88 | + |
| 89 | +## Ruby |
| 90 | +```ruby |
| 91 | +# Welcome to create a PR to complete the code of this language, thanks! |
| 92 | +``` |
| 93 | + |
| 94 | +## C |
| 95 | +```c |
| 96 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 97 | +``` |
| 98 | + |
| 99 | +## Kotlin |
| 100 | +```kotlin |
| 101 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 102 | +``` |
| 103 | + |
| 104 | +## Swift |
| 105 | +```swift |
| 106 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 107 | +``` |
| 108 | + |
| 109 | +## Rust |
| 110 | +```rust |
| 111 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 112 | +``` |
| 113 | + |
| 114 | +## Other languages |
| 115 | +``` |
| 116 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 117 | +``` |
| 118 | + |
| 119 | +## 中文题解 |
| 120 | +假设链表中待删除的节点是`d`,`d`的前一个节点是`p`,所以`p.next`就是`d`。 删除`d`,只需要把`p.next = p.next.next`。 |
| 121 | + |
| 122 | +但`head`节点前面没有节点,这就意味着需要对`head`节点进行特殊处理。 |
| 123 | + |
| 124 | +是否有方法能够让`head`节点的不再特殊呢? |
| 125 | + |
| 126 | +这样就不需要特殊处理`head`了。 |
| 127 | + |
| 128 | +办法是引入`dummy`节点,`dummy.next = head`。 |
0 commit comments