-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLeetcode25.java
74 lines (66 loc) · 1.86 KB
/
Leetcode25.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
69
70
71
72
73
74
public class Leetcode25 {
/**
* For this problem we have both iterative solution and recurisve
*/
public ListNode reverseKGroup(ListNode head,int k){
ListNode n1 = head, nkPlus = head;
for(int i = 0;i < k;i++){
// no enough node to reverse, just return
if(nkPlus == null)return head;
nkPlus = nkPlus.next;
}
// reverse the first group
ListNode newHead = reverse(n1, nkPlus);
// reverse the following group
n1.next = reverseKGroup(nkPlus, k);
return newHead;
}
/**
* Lets have a helper function that rever linked list from head to end [left,end)
* @param head
* @param end
* @return
*/
private ListNode reverse(ListNode head,ListNode end){
ListNode newHead = null;
while(head != end){
ListNode temp = head.next;
head.next = newHead;
newHead = head;
head = temp;
}
return newHead;
}
/**
* Iterative solution
*/
public ListNode reverseKGroup(ListNode head, int k) {
ListNode dummy = new ListNode();
dummy.next = head;
head = dummy;
while(head != null){
head = reverseNextK(head,k);
}
return dummy.next;
}
private ListNode reverseNextK(ListNode head, int k){
ListNode n1 = head.next;
ListNode nk = head;
for(int i = 0;i < k;i++){
nk = nk.next;
if(nk == null)return null;
}
ListNode nkPlus = nk.next;
ListNode newHead = null;
ListNode cur = n1;
while(cur != nkPlus){
ListNode temp = cur.next;
cur.next = newHead;
newHead = cur;
cur = temp;
}
head.next = newHead;
n1.next = nkPlus;
return n1;
}
}