Repository navigation
Expand file tree
/
Copy pathReverseNodesInKGroup.java
More file actions
67 lines (59 loc) · 1.64 KB
/
Copy pathReverseNodesInKGroup.java
File metadata and controls
67 lines (59 loc) · 1.64 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
package Novice_class;
// K 个一组翻转链表
public class ReverseNodesInKGroup {
public 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 ListNode reverseKGroup(ListNode head, int k) {
ListNode start = head;
ListNode end = getKGroupEnd(start , k);
if(end == null) {
return head;
}
// 说明有第一段
head = end;
reverse(start, end);
while(start.next != null) {
ListNode lastEnd = start;
start = lastEnd.next;
end = getKGroupEnd(start,k);
if(end == null) {
return head;
}
// 说明有第二段
reverse(start, end);
lastEnd.next = end;
}
return head;
}
// 得到一段区域的末尾节点
public static ListNode getKGroupEnd(ListNode start, int k) {
for (int i = 1; start != null && i < k; i++) {
start = start.next;
}
return start;
}
// 把这一段进行反转
public static void reverse(ListNode start, ListNode end) {
ListNode pre = null;
ListNode cur = start;
ListNode next = null;
while(pre != end) {
next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
start.next = cur;
}
}