-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReverseNodesInkGroup.java
68 lines (60 loc) · 1.69 KB
/
ReverseNodesInkGroup.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
import java.util.ArrayList;
import java.util.List;
/**
* Created by cpacm on 2017/5/10.
*/
public class ReverseNodesInkGroup {
public static void main(String[] args) {
ListNode head = new ListNode(1);
head.next = new ListNode(2);
head.next.next = new ListNode(3);
head.next.next.next = new ListNode(4);
System.out.println(reverseKGroup(head, 2));
}
public static ListNode reverseKGroup(ListNode head, int k) {
if (head == null || head.next == null) return head;
List<ListNode> nodeList = new ArrayList<>();
while (head != null) {
nodeList.add(head);
head = head.next;
}
int size = nodeList.size();
int start = 0;
while (size >= k) {
swapNodes(nodeList, start, start + k - 1);
size = size - k;
start = start + k;
}
ListNode v = nodeList.get(0);
ListNode temp = v;
for (int i = 1; i < nodeList.size(); i++) {
temp.next = nodeList.get(i);
temp = temp.next;
}
temp.next = null;
return v;
}
public static void swapNodes(List<ListNode> list, int start, int end) {
if (end >= list.size()) {
return;
}
while (start < end) {
ListNode temp = list.get(start);
list.set(start, list.get(end));
list.set(end, temp);
start++;
end--;
}
}
public static class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
@Override
public String toString() {
return val + "";
}
}
}