forked from yuduozhou/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathReverseNodesInKGroup.java
36 lines (36 loc) · 1.13 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
public class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
// Start typing your Java solution below
// DO NOT write main() function
if(k == 0 || k == 1) return head;
ListNode cur = head;
int length = 0;
while (cur != null){
cur = cur.next;
length++;
}
int multi = length / k;
if(multi == 0) return head;
ListNode preTail = null, curHead = null, curTail = null;
ListNode preNode = null, nextNode = null;
cur = head;
for(int j = 0; j < multi; j++) {
preNode = null;
for(int i = 0; i < k; i++) {
if(cur != null) {
nextNode = cur.next;
cur.next = preNode;
preNode = cur;
}
if(i == 0) curTail = cur;
if(i == (k - 1)) curHead = cur;
cur = nextNode;
}
if(preTail == null) head = curHead;
else preTail.next = curHead;
preTail = curTail;
}
curTail.next = cur;
return head;
}
}