Skip to content

Commit 26b6232

Browse files
authored
Merge pull request #843 from 0xff-dev/2487
Add solution and test-cases for problem 2487
2 parents 80c7613 + 9301aa7 commit 26b6232

File tree

4 files changed

+73
-24
lines changed

4 files changed

+73
-24
lines changed

leetcode/2401-2500/2487.Remove-Nodes-From-Linked-List/README.md

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
# [2487.Remove Nodes From Linked List][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given the `head` of a linked list.
75

8-
**Example 1:**
6+
Remove every node which has a node with a greater value anywhere to the right side of it.
97

10-
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
8+
Return the `head` of the modified linked list.
149

15-
## 题意
16-
> ...
10+
**Example 1:**
1711

18-
## 题解
12+
![1](./drawio.png)
1913

20-
### 思路1
21-
> ...
22-
Remove Nodes From Linked List
23-
```go
14+
```
15+
Input: head = [5,2,13,3,8]
16+
Output: [13,8]
17+
Explanation: The nodes that should be removed are 5, 2 and 3.
18+
- Node 13 is to the right of node 5.
19+
- Node 13 is to the right of node 2.
20+
- Node 8 is to the right of node 3.
2421
```
2522

23+
**Example 2:**
24+
25+
```
26+
Input: head = [1,1,1,1]
27+
Output: [1,1,1,1]
28+
Explanation: Every node has value 1, so no nodes are removed.
29+
```
2630

2731
## 结语
2832

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
type ListNode struct {
4+
Val int
5+
Next *ListNode
6+
}
7+
8+
func Solution(head *ListNode) *ListNode {
9+
stack := []*ListNode{head}
10+
w := head.Next
11+
for ; w != nil; w = w.Next {
12+
for len(stack) > 0 && stack[len(stack)-1].Val < w.Val {
13+
stack = stack[:len(stack)-1]
14+
}
15+
stack = append(stack, w)
16+
}
17+
for i := 1; i < len(stack); i++ {
18+
stack[i-1].Next = stack[i]
19+
}
20+
return stack[0]
521
}

leetcode/2401-2500/2487.Remove-Nodes-From-Linked-List/Solution_test.go

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,41 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs *ListNode
14+
expect *ListNode
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", &ListNode{
17+
Val: 5,
18+
Next: &ListNode{
19+
Val: 2,
20+
Next: &ListNode{
21+
Val: 13,
22+
Next: &ListNode{
23+
Val: 3,
24+
Next: &ListNode{Val: 8},
25+
},
26+
},
27+
},
28+
}, &ListNode{Val: 13, Next: &ListNode{Val: 8}}},
29+
{"TestCase2", &ListNode{
30+
Val: 1,
31+
Next: &ListNode{
32+
Val: 1,
33+
Next: &ListNode{
34+
Val: 1,
35+
Next: &ListNode{Val: 1},
36+
},
37+
},
38+
}, &ListNode{
39+
Val: 1,
40+
Next: &ListNode{
41+
Val: 1,
42+
Next: &ListNode{
43+
Val: 1,
44+
Next: &ListNode{Val: 1},
45+
},
46+
},
47+
}},
1948
}
2049

2150
// 开始测试
@@ -30,10 +59,10 @@ func TestSolution(t *testing.T) {
3059
}
3160
}
3261

33-
// 压力测试
62+
// 压力测试
3463
func BenchmarkSolution(b *testing.B) {
3564
}
3665

37-
// 使用案列
66+
// 使用案列
3867
func ExampleSolution() {
3968
}
Loading

0 commit comments

Comments
 (0)