-
Notifications
You must be signed in to change notification settings - Fork 0
/
025_ReverseNodesInKGroup25.java
150 lines (132 loc) · 4.83 KB
/
025_ReverseNodesInKGroup25.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
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
140
141
142
143
144
145
146
147
148
149
150
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
if (k <= 1) return head;
int len = 1;
ListNode firstTail = head;
//Loop to find the tail of the group starting by head
//The group must be k-length
for(; firstTail != null && len < k; len++) firstTail = firstTail.next;
if (firstTail == null || len < k) return head; //k is greater than num of total nodes, or the last nodes don't make a k-length group (a non-group)
ListNode tail, auxHead, aux1, aux2, aux3;
while (len == k) {
auxHead = head;
aux1 = head;
aux2 = aux1.next;
aux3 = null;
//Loop to reverse a group
//It takes k-1 iterations (for the group {1 -> 2}, we just have to make {2 -> 1}, which takes one iteration)
//This loop is to reverse a group, we still have to join 2 different groups, which takes place outside this loop
for(int i = 0; i < k-1; i++) {
aux3 = aux2.next;
aux2.next = aux1;
aux1 = aux2;
aux2 = aux3;
}
//Since aux3 is the next node which the head of the actual group had before it was reversed, we find the tail of this new group starting by aux3
//The code is the same as above
len = 1;
head = aux3;
tail = head;
for(; tail != null && len < k; len++) tail = tail.next;
//There wasn't enough nodes to make a k-length group (a non-group), so we decrease its length so it doesn't count the null node
//Also, the tail of the previous calculated group will point to the head of the non-group
if (tail == null) {
tail = head;
len--;
}
auxHead.next = tail;
}
return firstTail;
}
}
***************************************************************************************************
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
ListNode begin;
if (head==null || head.next ==null || k==1)
return head;
ListNode dummyhead = new ListNode(-1);
dummyhead.next = head;
begin = dummyhead;
int i=0;
while (head != null){
i++;
if (i%k == 0){
begin = reverse(begin, head.next);
head = begin.next;
} else {
head = head.next;
}
}
return dummyhead.next;
}
public ListNode reverse(ListNode begin, ListNode end){
ListNode curr = begin.next;
ListNode next, first;
ListNode prev = begin;
first = curr;
while (curr!=end){
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
begin.next = prev;
first.next = curr;
return first;
}
}
*************************************************************************************************************************
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
ListNode newHead = null;
ListNode newPtr = null;
ListNode current = head;
ListNode first = null;
ListNode cap = null;
while(current != null){
// Gonna need this to reverse the k group
first = current;
// Get the k group
for(int i = 0; i < k-1; i++){
if(current == null) break;
current = current.next;
}
// If we have a full k group
if(current != null){
cap = current.next;
current.next = null;
ListNode reversed = reverse(first);
// Add to the end or head of our new linked list
if(newHead == null){
newHead = reversed;
newPtr = newHead;
}
else newPtr.next = reversed;
while(newPtr != null && newPtr.next != null) newPtr = newPtr.next;
}
// HANDLE REMAINING NODES
else{
newPtr.next = first;
break;
}
// Resume where we left off
current = cap;
}
return newHead;
}
public ListNode reverse(ListNode head) {
if(head == null) return null;
ListNode prev = null;
ListNode current = head;
ListNode temp = null;
while(current != null){
temp = current.next;
current.next = prev;
prev = current;
current = temp;
}
head.next = null;
return prev;
}
}