-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution_25.java
More file actions
68 lines (62 loc) · 1.49 KB
/
Solution_25.java
File metadata and controls
68 lines (62 loc) · 1.49 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
package com.hilbert25.leetcode;
/**
* @author : hilbert25
* @version 创建时间:2017年4月10日 下午10:33:55 LeetCode com.hilbert25.leetcode
* Solution_25
*/
public class Solution_25 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int count = 3;
ListNode[] nodeArr = new ListNode[count];
for (int i = 0; i < count; i++)
nodeArr[i] = new ListNode(i);
for (int i = 0; i < count - 1; i++)
nodeArr[i].next = nodeArr[i + 1];
ListNode node = reverseKGroup(nodeArr[0], 3);
}
public static class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
public static ListNode reverseKGroup(ListNode head, int k) {
if (head == null || head.next == null || k == 1)
return head;
ListNode res = new ListNode(-1);
res.next = head;
ListNode preTail = res;
ListNode cur = head;
ListNode curHead = head;
ListNode curNext = head.next;
ListNode curTail = head;
ListNode test = head;
while (test != null) {
test = preTail.next;
for (int i = 1; i < k; i++) {
test = test.next;
if (test == null)
return res.next;
}
curHead = preTail.next;
cur = curHead.next;
curNext = cur.next;
curTail = curHead;
test = curHead;
for (int i = 1; i < k; i++) {
curTail.next = curNext;
cur.next = curHead;
preTail.next = cur;
curHead = cur;
cur = curNext;
if (cur == null)
return res.next;
curNext = cur.next;
}
preTail = curTail;
}
return res.next;
}
}