-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathw3.q15
46 lines (37 loc) · 975 Bytes
/
w3.q15
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
class Solution {
int iteration;
public:
ListNode* reverse(ListNode* head, int k, int it) {
if(it > iteration || head == NULL) {
return head;
}
ListNode* curr = head;
ListNode* prev = NULL;
ListNode* next = NULL;
int count = 0;
while(curr != NULL & count < k) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
count++;
}
if(next != NULL) {
head->next = reverse(next, k, it+1);
}
return prev;
}
ListNode* reverseKGroup(ListNode* head, int k) {
if(head == NULL) {
return NULL;
}
ListNode* curr = head;
int n = 0;
while(curr != NULL) {
curr = curr->next;
n++;
}
iteration = n/k;
return reverse(head,k,1);
}
};