-
Notifications
You must be signed in to change notification settings - Fork 1
/
No25ReverseNodesInKGroup.java
180 lines (159 loc) · 5.25 KB
/
No25ReverseNodesInKGroup.java
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
178
179
180
package leetCode.repository;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
/**
* K 个一组翻转链表
* <a href="https://leetcode-cn.com/problems/reverse-nodes-in-k-group/">address</a>
*
* @author jiangxiewei
* @since 2021/5/12
*/
public class No25ReverseNodesInKGroup {
public static void main(String[] args) {
//输出:[2,1,4,3,5]
new No25ReverseNodesInKGroup().reverseKGroup(ListNode.packageList(1, 2, 3, 4, 5), 2).print();
//输出:[3,2,1,4,5]
new No25ReverseNodesInKGroup().reverseKGroup(ListNode.packageList(1, 2, 3, 4, 5), 3).print();
//输出:[1,2,3,4,5]
new No25ReverseNodesInKGroup().reverseKGroup(ListNode.packageList(1, 2, 3, 4, 5), 1).print();
//输出:[1]
new No25ReverseNodesInKGroup().reverseKGroup(ListNode.packageList(1), 1).print();
}
public ListNode reverseKGroup(ListNode head, int k){
return new 迭代模式进行翻转().reverseKGroup(head, k);
}
/**
* 解法接口
*/
private interface ReverseKGroup {
ListNode reverseKGroup(ListNode head, int k);
}
/**
* 重做,不用递归实现. 然而结果并没有变优雅.
*/
private static class 迭代模式进行翻转 implements ReverseKGroup{
@Override
public ListNode reverseKGroup(ListNode head, int k) {
if (k == 1) {
return head;
}
int count = 1;
ListNode resultBefore, beforeStart = new ListNode(-1, head), start = head, p = head, end, afterEnd;
resultBefore = beforeStart;
while (p.next != null) {
p = p.next;
count++;
if (count >= k) {
end = p;
afterEnd = p.next;
//达到k个一组了,start成为新end,end成为新start
reverse(start, end);
ListNode t = start;
start = end;
end = t;
//将翻转后的区间重新接入
beforeStart.next = start;
end.next = afterEnd;
//重置beforeStart和start
beforeStart = end;
start = end.next;
p = end;
count = 0;
}
}
return resultBefore.next;
}
/**
* 翻转并返回新的头结点
*/
public ListNode reverse(ListNode node, ListNode end) {
if (node == end) {
return node;
}
reverse(node.next, end).next = node;
return node;
}
}
/**
* 递归方式实现,个人感觉写的很不优雅(写的什么辣鸡玩意).
*/
private static class 自己想的递归模式 implements ReverseKGroup {
@Override
public ListNode reverseKGroup(ListNode head, int k) {
var cur = head;
while (cur != null) {
recursion(cur, k);
if (isBreak) {
break;
}
cur.next = deepestRecursionTail;
beforeRecursionHead = cur;
cur = cur.next;
}
return firstResult == null ? head : firstResult;
}
private ListNode firstResult = null;
private boolean isBreak = false;
private ListNode beforeRecursionHead = null;
private ListNode deepestRecursionTail = null;
public ListNode recursion(ListNode node, int cur) {
if (cur > 0 && node == null) {
isBreak = true;
} else if (cur == 1) {
if (beforeRecursionHead != null) {
beforeRecursionHead.next = node;
}
if (firstResult == null) {
firstResult = node;
}
deepestRecursionTail = node.next;
} else {
recursion(node.next, cur - 1);
if (isBreak) {
return node;
}
if (node.next != null) {
node.next.next = node;
}
}
return node;
}
}
/**
* 莫赋值
*/
private static class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
public static ListNode packageList(int... values) {
var head = new ListNode();
ListNode cur = head;
for (int i = 0; i < values.length; i++) {
cur.val = values[i];
if (i<values.length-1) {
cur.next = new ListNode();
}
cur = cur.next;
}
return head;
}
public void print() {
List<Integer> linked = new LinkedList<>();
ListNode cur = this;
do {
linked.add(cur.val);
} while ((cur = cur.next) != null);
System.out.println(Arrays.toString(linked.toArray()));
}
}
}