Skip to content

Commit e9792f9

Browse files
aQuaaQua
aQua
authored and
aQua
committed
92 accepted
1 parent a23491b commit e9792f9

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# [92. Reverse Linked List II](https://leetcode.com/problems/reverse-linked-list-ii/)
2+
3+
## 题目
4+
Reverse a linked list from position m to n. Do it in-place and in one-pass.
5+
6+
```
7+
For example:
8+
Given 1->2->3->4->5->NULL, m = 2 and n = 4,
9+
return 1->4->3->2->5->NULL.
10+
```
11+
12+
Note: Given m, n satisfy the following condition:
13+
1. 1 ≤ m ≤ n ≤ length of list.
14+
1. head 的序号为 1
15+
16+
## 解题思路
17+
18+
见程序注释
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package Problem0092
2+
3+
// ListNode is Definition for singly-linked list.
4+
type ListNode struct {
5+
Val int
6+
Next *ListNode
7+
}
8+
9+
func reverseBetween(head *ListNode, m int, n int) *ListNode {
10+
if m == n {
11+
return head
12+
}
13+
14+
// 添加 headPre 是为了 m >= 2
15+
//
16+
headPre := &ListNode{}
17+
headPre.Next = head
18+
m++
19+
n++
20+
21+
mPre, mNode, nNext := split(headPre, m, n)
22+
23+
h, e := reverse(mNode)
24+
mPre.Next = h
25+
e.Next = nNext
26+
27+
return headPre.Next
28+
}
29+
30+
func split(head *ListNode, m, n int) (mPre, mNode, nNext *ListNode) {
31+
i := 1
32+
for head != nil {
33+
if i == m-1 {
34+
mPre = head
35+
mNode = head.Next
36+
}
37+
if i == n {
38+
nNext = head.Next
39+
head.Next = nil
40+
break
41+
}
42+
head = head.Next
43+
i++
44+
}
45+
46+
return
47+
}
48+
49+
func reverse(head *ListNode) (h, e *ListNode) {
50+
if head == nil || head.Next == nil {
51+
return head, head
52+
}
53+
54+
var end *ListNode
55+
56+
h, end = reverse(head.Next)
57+
end.Next = head
58+
e = head
59+
60+
return
61+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package Problem0092
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
type question struct {
11+
para
12+
ans
13+
}
14+
15+
// para 是参数
16+
type para struct {
17+
head []int
18+
m int
19+
n int
20+
}
21+
22+
// ans 是答案
23+
type ans struct {
24+
one []int
25+
}
26+
27+
func Test_Problem0092(t *testing.T) {
28+
ast := assert.New(t)
29+
30+
qs := []question{
31+
32+
question{
33+
para{
34+
[]int{1, 2, 3, 4, 5},
35+
2,
36+
2,
37+
},
38+
ans{
39+
[]int{1, 2, 3, 4, 5},
40+
},
41+
},
42+
43+
question{
44+
para{
45+
[]int{1, 2, 3, 4, 5},
46+
2,
47+
4,
48+
},
49+
ans{
50+
[]int{1, 4, 3, 2, 5},
51+
},
52+
},
53+
54+
// 如需多个测试,可以复制上方元素。
55+
}
56+
57+
for _, q := range qs {
58+
a, p := q.ans, q.para
59+
fmt.Printf("~~%v~~\n", p)
60+
61+
ast.Equal(a.one, l2s(reverseBetween(s2l(p.head), p.m, p.n)), "输入:%v", p)
62+
}
63+
}
64+
65+
// convert *ListNode to []int
66+
func l2s(head *ListNode) []int {
67+
res := []int{}
68+
69+
for head != nil {
70+
res = append(res, head.Val)
71+
head = head.Next
72+
}
73+
74+
return res
75+
}
76+
77+
// convert []int to *ListNode
78+
func s2l(nums []int) *ListNode {
79+
if len(nums) == 0 {
80+
return nil
81+
}
82+
83+
res := &ListNode{
84+
Val: nums[0],
85+
}
86+
temp := res
87+
for i := 1; i < len(nums); i++ {
88+
temp.Next = &ListNode{
89+
Val: nums[i],
90+
}
91+
temp = temp.Next
92+
}
93+
94+
return res
95+
}

0 commit comments

Comments
 (0)