forked from Blankj/awesome-java-leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
45 lines (41 loc) · 1.16 KB
/
Solution.java
File metadata and controls
45 lines (41 loc) · 1.16 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
package com.blankj.hard._025;
import com.blankj.structure.ListNode;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2017/10/16
* desc :
* </pre>
*/
public class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
if (head == null || k == 1) return head;
ListNode node = new ListNode(0), pre = node;
node.next = head;
for (int i = 1; head != null; ++i) {
if (i % k == 0) {
pre = reverse(pre, head.next);
head = pre.next;
} else {
head = head.next;
}
}
return node.next;
}
private ListNode reverse(ListNode pre, ListNode next) {
ListNode head = pre.next;
ListNode move = head.next;
while (move != next) {
head.next = move.next;
move.next = pre.next;
pre.next = move;
move = head.next;
}
return head;
}
public static void main(String[] args) {
Solution solution = new Solution();
ListNode.print(solution.reverseKGroup(ListNode.createTestData("[1,2,3,4,5,6,7,8]"), 3));
}
}