-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathReverseNodesInKGroup.java
60 lines (59 loc) · 1.81 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* Given a linked list, reverse the nodes of a linked list k at a time and
* return its modified list. is a positive integer and is less than or equal to
* the length of the linked list. If the number of nodes is not a multiple of k
* then left-out nodes in the end should remain as it is.
*/
class Solution {
ListNode reverse(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode curr = head;
ListNode prev = null;
ListNode next = head.next;
while (curr.next != null) {
curr.next = prev;
prev = curr;
curr = next;
next = next.next;
}
curr.next = prev;
return curr;
}
public ListNode reverseKGroup(ListNode head, int k) {
if (head == null || head.next == null) {
return head;
}
ListNode newHead = null;
ListNode newTail = null;
ListNode start = head;
ListNode prev = null;
ListNode curr = head;
ListNode next = head.next;
int k1 = k;
while (curr != null) {
prev = curr;
curr = curr.next;
k1--;
if (k1 == 0) {
prev.next = null;
if (newHead == null) {
newHead = reverse(head);
newTail = head;
} else {
newTail.next = reverse(start);
newTail = start;
}
start = curr;
prev = curr;
if (curr != null)
curr = curr.next;
k1 = k - 1;
}
}
if (newTail != null)
newTail.next = start;
return newHead;
}
}