-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLeetCode-25-Reverse-Nodes-in-k-Group.java
More file actions
139 lines (111 loc) · 3.59 KB
/
LeetCode-25-Reverse-Nodes-in-k-Group.java
File metadata and controls
139 lines (111 loc) · 3.59 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
// 1. Iterative
public ListNode reverseKGroup(ListNode head, int k) {
if(head == null || head.next == null || k <= 1) return head;
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode prev = dummy;
while(prev.next != null) prev = reverseNextK(prev, k);
return dummy.next;
}
private ListNode reverseNextK(ListNode prev, int k){
// check if there is enough k nodes
ListNode temp = prev;
for(int i = 0; i < k; i++){
if(temp.next == null) return temp;
temp = temp.next;
}
ListNode curr = prev.next;
ListNode result = prev.next;
for(int i = 0; i < k; i++){
ListNode next = curr.next;
curr.next = prev.next;
prev.next = curr;
curr = next;
}
result.next = curr;
return result;
}
// 2.Iterative
public ListNode reverseKGroup(ListNode head, int k) {
if (head ==null || k==1) return head;
ListNode dummy = new ListNode (0);
dummy.next = head;
ListNode pre = dummy;
int i = 0;
while (head != null){
i++;
if (i % k == 0){
pre = reverse(pre, head.next);
head = pre.next;
}
else {
head = head.next;
}
}
return dummy.next;
}
/**
* Reverse a link list between pre and next exclusively
* an example:
* a linked list:
* 0->1->2->3->4->5->6
* | |
* pre next
* after call pre = reverse(pre, next)
*
* 0->3->2->1->4->5->6
* | |
* pre next
* @param pre
* @param next
* @return the reversed list's last node, which is the precedence of parameter next
*/
public ListNode reverse(ListNode pre, ListNode next){
ListNode last = pre.next;
ListNode cur = last.next;
while (cur != next){
last.next = cur.next;
cur.next = pre.next;
pre.next = cur;
cur = last.next;
}
return last;
}
// 3.Recursively
public ListNode reverseKGroup(ListNode head, int k) {
ListNode prev = new ListNode(-1);
prev.next = head;
ListNode next = prev.next; // next is the last node in k group
int count = 1;
// get the Kth node or null if there is no enough K nodes left in the list
while (next != null && count < k) {
next = next.next;
count++;
}
// means there is no enough K nodes in this group, directly return head
if (next == null) return head;
next.next = reverseKGroup(next.next, k); // next is the last node in this k group, next.next is first node in next k group
reverse(prev, next.next);
return prev.next;
}
private ListNode reverse(ListNode prev, ListNode next) {
ListNode last = prev.next;
ListNode curr = last.next;
while (curr != next) {
last.next = curr.next;
curr.next = prev.next;
prev.next = curr;
curr = last.next;
}
return prev.next;
}
}