-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathReverse nodes in k-groups.cpp
41 lines (39 loc) · 1.17 KB
/
Reverse nodes in k-groups.cpp
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
#include <bits/stdc++.h>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k) {
if(head==NULL || head->next==NULL || k==1) return head;
ListNode* current = head, *new_head = NULL;
int length = 0;
while(current != NULL) {
length++;
current = current->next;
}
ListNode* previous = NULL, *next = NULL, *tail1 = NULL, *tail2 = head;
current = head;
while(length >= k) {
for(int i = 0; i < k; i++) {
next = current->next;
current->next = previous;
previous = current;
current = next;
}
if(new_head == NULL) new_head = previous;
if(tail1 != NULL) tail1->next = previous;
tail2->next = current;
tail1 = tail2;
tail2 = current;
previous = NULL;
length -= k;
}
return new_head;
}
};