Skip to content

Commit 275a673

Browse files
committed
203-remove-linked-list-elements.md Added Python solution and Chinese thoughts.
1 parent 385654d commit 275a673

4 files changed

Lines changed: 132 additions & 1 deletion

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ You can skip the more difficult problems and do them later.
2121
- [59. Spiral Matrix II](solutions/1-1000/59-spiral-matrix-ii.md) was solved in _Python, Java, JavaScript, C#_.
2222
- [503. Next Greater Element II](solutions/1-1000/503-next-greater-element-ii.md) was solved in _Python, Java, C++, JavaScript, C#, Go, Ruby_.
2323

24+
# Linked List
25+
- [203. Remove Linked List Elements](solutions/1-1000/203-remove-linked-list-elements.md) was solved in _Python, Java, C++, JavaScript, C#, Go, Ruby_.
26+
2427
# Dynamic Programming
2528
## Basics
2629
- [509. Fibonacci Number](solutions/1-1000/509-fibonacci-number.md) was solved in _Python, Java, C++, JavaScript, C#, Go, Ruby_.

images/examples/203_1.jpg

22.9 KB
Loading
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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+
![](../../images/examples/203_1.jpg)
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`

solutions/dynamic_programming/unorganized.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ If you want to solve problems in the most understandable way, please look for Co
33

44
## Array
55
* Array is consecutive in memory.
6-
* Delete a item of array will call the latter items move 1 to left. So it is `O(n)` time complexity.
6+
* Cannot delete an item. Actually, it is overwrite. Delete a item of array will call the latter items move 1 to left. So it is `O(n)` time complexity.
77
* C++ 2D array is also consecutive. But Java is not.
88

99
## Binary tree unified stack iteration

0 commit comments

Comments
 (0)