Skip to content

Commit e458a8d

Browse files
committed
feat(solutions): add rotate_list
1 parent fca18b6 commit e458a8d

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
| # | Problem | Solution | Difficulty | Single Repetition Duration | LeetCode Run Time |
2626
| ---: | :----- | :--------: | :----------: | ----------: | ----------: |
27+
|61|[Rotate List][Solutions-61]|[WindomZ][Solutions-61-Go]|Medium|[34.0 ns/op/2 test cases][Solutions-61-Test]|? ms|
2728
|59|[Spiral Matrix II][Solutions-59]|[WindomZ][Solutions-59-Go]|Medium|[90.3 ns/op/3 test cases][Solutions-59-Test]|? ms|
2829
|58|[Length of Last Word][Solutions-58]|[WindomZ][Solutions-58-Go]|Easy|[4.02 ns/op/5 test cases][Solutions-58-Test]|0 ms|
2930
|56|[Merge Intervals][Solutions-56]|[WindomZ][Solutions-56-Go]|Medium|[154 ns/op/5 test cases][Solutions-56-Test]|19 ms|
@@ -113,6 +114,9 @@ Welcome to report bugs, suggest ideas and discuss on [issues page](https://githu
113114
### Support
114115
If you like it then you can put a :star:Star on it.
115116

117+
[Solutions-61-Test]:solutions/rotate_list/rotatelist_test.go#L29
118+
[Solutions-61-Go]:solutions/rotate_list/rotatelist.go
119+
[Solutions-61]:https://leetcode.com/rotate-list/
116120
[Solutions-59-Test]:solutions/spiral_matrix_ii/spiralmatrixii_test.go#L22
117121
[Solutions-59-Go]:solutions/spiral_matrix_ii/spiralmatrixii.go
118122
[Solutions-59]:https://leetcode.com/spiral-matrix-ii/

solutions/rotate_list/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# 61. Rotate List
2+
3+
## Description
4+
5+
Given a list, rotate the list to the right by _k_ places, where _k_ is non-negative.
6+
7+
For example:
8+
9+
Given `1->2->3->4->5->NULL` and _k_ = `2`,
10+
11+
return `4->5->1->2->3->NULL`.

solutions/rotate_list/rotatelist.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package rotatelist
2+
3+
// Definition for singly-linked list.
4+
type ListNode struct {
5+
Val int
6+
Next *ListNode
7+
}
8+
9+
func rotateRight(head *ListNode, k int) *ListNode {
10+
if head == nil || k <= 0 {
11+
return head
12+
}
13+
14+
size := 1 // number of nodes
15+
tail := head
16+
17+
// get the tail node
18+
for ; tail.Next != nil; size++ {
19+
tail = tail.Next
20+
}
21+
tail.Next = head // circle the nodes
22+
23+
if k %= size; k != 0 {
24+
// the tail node is the (size-k)th node (1st node is head)
25+
for i := 0; i < size-k; i++ {
26+
tail = tail.Next
27+
}
28+
}
29+
30+
head = tail.Next
31+
tail.Next = nil
32+
return head
33+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package rotatelist
2+
3+
import (
4+
"testing"
5+
6+
"github.com/WindomZ/testify/assert"
7+
)
8+
9+
func Test_rotateRight(t *testing.T) {
10+
var node *ListNode
11+
assert.Equal(t, node, rotateRight(node, 0))
12+
13+
for i := 5; i >= 1; i-- {
14+
node = &ListNode{
15+
Val: i,
16+
Next: node,
17+
}
18+
}
19+
node = rotateRight(node, 2)
20+
val := []int{4, 5, 1, 2, 3}
21+
for i := 0; i < 5; i++ {
22+
if assert.NotEmpty(t, node) {
23+
assert.Equal(t, val[i], node.Val)
24+
node = node.Next
25+
}
26+
}
27+
}
28+
29+
func Benchmark_rotateRight(b *testing.B) {
30+
b.StopTimer()
31+
b.ReportAllocs()
32+
33+
f := func() (node *ListNode) {
34+
for i := 5; i >= 1; i-- {
35+
node = &ListNode{
36+
Val: i,
37+
Next: node,
38+
}
39+
}
40+
return
41+
}
42+
43+
b.StartTimer()
44+
b.RunParallel(func(pb *testing.PB) {
45+
for pb.Next() {
46+
rotateRight(nil, 0)
47+
48+
rotateRight(f(), 2)
49+
}
50+
})
51+
}

0 commit comments

Comments
 (0)