Skip to content

Commit 2dcec46

Browse files
aQuaaQua
aQua
authored and
aQua
committed
61 runtime error
1 parent f818bbd commit 2dcec46

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

Algorithms/0061.rotate-list/rotate-list.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package Problem0061
22

3-
import (
4-
"fmt"
5-
)
6-
73
// ListNode 是题目预定义的节点
84
type ListNode struct {
95
Val int
@@ -12,22 +8,23 @@ type ListNode struct {
128

139
func rotateRight(head *ListNode, k int) *ListNode {
1410
end := head
15-
cnt := 1
11+
// size 是 List 的长度
12+
size := 1
1613
for end.Next != nil {
1714
end = end.Next
18-
cnt++
15+
size++
1916
}
20-
fmt.Println(cnt)
2117

2218
end.Next = head
23-
fmt.Println(l2s(end))
2419

25-
cnt -= k % cnt
20+
// steps 是 head 需要移动的步数
21+
// k%size 因为 k 很可能大于 size
22+
steps := size - k%size
2623

27-
for cnt > 1 {
24+
for steps > 0 {
2825
head = head.Next
2926
end = end.Next
30-
cnt--
27+
steps--
3128
}
3229

3330
end.Next = nil

Algorithms/0061.rotate-list/rotate-list_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ func Test_Problem0061(t *testing.T) {
2828

2929
qs := []question{
3030

31+
question{
32+
para{
33+
[]int{},
34+
0,
35+
},
36+
ans{
37+
[]int{},
38+
},
39+
},
40+
3141
question{
3242
para{
3343
[]int{1, 2, 3, 4, 5},

0 commit comments

Comments
 (0)