-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path25-ReverseNodesInKGroup.go
More file actions
177 lines (158 loc) · 4.72 KB
/
25-ReverseNodesInKGroup.go
File metadata and controls
177 lines (158 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
// 25. Reverse Nodes in k-Group
// Given the head of a linked list, reverse the nodes of the list k at a time, and return the modified list.
// k is a positive integer and is less than or equal to the length of the linked list.
// If the number of nodes is not a multiple of k then left-out nodes, in the end, should remain as it is.
// You may not alter the values in the list's nodes, only nodes themselves may be changed.
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2020/10/03/reverse_ex1.jpg" />
// Input: head = [1,2,3,4,5], k = 2
// Output: [2,1,4,3,5]
// Example 2:
// <img src="https://assets.leetcode.com/uploads/2020/10/03/reverse_ex2.jpg" />
// Input: head = [1,2,3,4,5], k = 3
// Output: [3,2,1,4,5]
// Constraints:
// The number of nodes in the list is n.
// 1 <= k <= n <= 5000
// 0 <= Node.val <= 1000
import "fmt"
type ListNode struct {
Val int
Next *ListNode
}
// 打印链表
func printListNode(l *ListNode) {
if nil == l {
return
}
for {
if nil == l.Next {
fmt.Print(l.Val)
break
} else {
fmt.Print(l.Val, " -> ")
}
l = l.Next
}
fmt.Println()
}
// 数组创建链表
func makeListNode(arr []int) *ListNode {
if (len(arr) == 0) {
return nil
}
var l = (len(arr) - 1)
var head = &ListNode{arr[l], nil}
for i := l - 1; i >= 0; i-- {
var n = &ListNode{arr[i], head}
head = n
}
return head
}
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func reverseKGroup(head *ListNode, k int) *ListNode {
cnt := 0
// 统计节点个数
for p := head; p != nil; p = p.Next {
cnt++
}
dummy := &ListNode{-1, head}
reversedListTail := dummy
pre, cur := (*ListNode)(nil), head
for cnt >= k {
for i := 0; i < k; i++ {
nxt := cur.Next
cur.Next = pre
pre = cur
cur = nxt
}
tail := reversedListTail.Next
tail.Next = cur
reversedListTail.Next = pre
reversedListTail = tail
cnt -= k
}
return dummy.Next
}
func reverseKGroup1(head *ListNode, k int) *ListNode {
cur := head
index:=0
var start,lastEnd,res *ListNode
reverse := func (start,end *ListNode) *ListNode {
cur := start
var last *ListNode
for cur != end {
next := cur.Next
if last == nil {
cur.Next = end
} else {
cur.Next = last
}
last = cur
cur = next
}
return last
}
for cur != nil {
index++
if index == 1 {
start = cur
}
cur = cur.Next
if index == k {
// tmp := start
h := reverse(start,cur)
if res == nil {
res = h
} else {
lastEnd.Next = h
}
lastEnd = start
start = cur
index=0
}
}
if res == nil {
res = head
}
return res
}
func main() {
l1 := makeListNode([]int{1,2,3,4,5})
fmt.Println("before: ")
printListNode(l1) // 1 -> 2 -> 3 -> 4 -> 5
fmt.Println("after: ")
printListNode(reverseKGroup(l1, 2)) // [2,1,4,3,5] [2 -> 1] -> [4 -> 3] -> 5
l2 := makeListNode([]int{1,2,3,4,5})
fmt.Println("before: ")
printListNode(l2) // 1 -> 2 -> 3 -> 4 -> 5
fmt.Println("after: ")
printListNode(reverseKGroup(l2, 3)) // [3,2,1,4,5] [3 -> 2 -> 1] -> 4 -> 5
l3 := makeListNode([]int{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15})
fmt.Println("before: ")
printListNode(l3) // 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> 11 -> 12 -> 13 -> 14 -> 15
fmt.Println("after: ")
printListNode(reverseKGroup(l3, 3)) // [3 -> 2 -> 1] -> [6 -> 5 -> 4] -> [9 -> 8 -> 7] -> [12 -> 11 -> 10] -> [15 -> 14 -> 13]
l11 := makeListNode([]int{1,2,3,4,5})
fmt.Println("before: ")
printListNode(l11) // 1 -> 2 -> 3 -> 4 -> 5
fmt.Println("after: ")
printListNode(reverseKGroup1(l11, 2)) // [2,1,4,3,5] [2 -> 1] -> [4 -> 3] -> 5
l12 := makeListNode([]int{1,2,3,4,5})
fmt.Println("before: ")
printListNode(l12) // 1 -> 2 -> 3 -> 4 -> 5
fmt.Println("after: ")
printListNode(reverseKGroup1(l12, 3)) // [3,2,1,4,5] [3 -> 2 -> 1] -> 4 -> 5
l13 := makeListNode([]int{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15})
fmt.Println("before: ")
printListNode(l13) // 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> 11 -> 12 -> 13 -> 14 -> 15
fmt.Println("after: ")
printListNode(reverseKGroup1(l13, 3)) // [3 -> 2 -> 1] -> [6 -> 5 -> 4] -> [9 -> 8 -> 7] -> [12 -> 11 -> 10] -> [15 -> 14 -> 13]
}