Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit f94661e

Browse files
aQuaaQua
authored andcommitted
143 added
1 parent b6e50c8 commit f94661e

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# [143. Reorder List](https://leetcode.com/problems/reorder-list/)
2+
3+
## 题目
4+
Given a singly linked list L: L0->L1->…->Ln-1->Ln,
5+
6+
reorder it to: L0->Ln->L1->Ln-1->L2->Ln-2->…
7+
8+
You must do this in-place without altering the nodes' values.
9+
10+
For example, Given {1,2,3,4}, reorder it to {1,4,2,3}.
11+
12+
## 解题思路
13+
14+
见程序注释
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package Problem0143
2+
3+
import (
4+
"github.com/aQuaYi/LeetCode-in-Golang/kit"
5+
)
6+
7+
type ListNode = kit.ListNode
8+
func reorderList(head *ListNode) {
9+
10+
11+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package Problem0143
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/aQuaYi/LeetCode-in-Golang/kit"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
// tcs is testcase slice
13+
var tcs = []struct {
14+
head []int
15+
ans []int
16+
}{
17+
18+
{
19+
[]int{1, 2, 3, 4},
20+
[]int{1, 4, 2, 3},
21+
},
22+
23+
// 可以有多个 testcase
24+
}
25+
26+
func Test_reorderList(t *testing.T) {
27+
ast := assert.New(t)
28+
29+
for _, tc := range tcs {
30+
fmt.Printf("~~%v~~\n", tc)
31+
32+
head := kit.Slice2List(tc.head)
33+
reorderList(head)
34+
ans := kit.List2Slice(head)
35+
36+
ast.Equal(tc.ans, ans, "输入:%v", tc)
37+
}
38+
}
39+
40+
func Benchmark_reorderList(b *testing.B) {
41+
for i := 0; i < b.N; i++ {
42+
for _, tc := range tcs {
43+
reorderList(kit.Slice2List(tc.head))
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)