-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq_025_reverse_k_group.cpp
More file actions
53 lines (50 loc) · 1.5 KB
/
q_025_reverse_k_group.cpp
File metadata and controls
53 lines (50 loc) · 1.5 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
/**
* Definition for singly-linked list.
* 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:
// 1. create a method to reverse the list, given head and tail
ListNode* reverse_section(ListNode* const begin, ListNode* const end_next) {
auto new_end = begin;
auto new_begin = begin;
ListNode* prev = nullptr;
while (new_begin != end_next) {
auto next = new_begin->next;
new_begin->next = prev;
prev = new_begin;
new_begin = next;
}
new_end->next = end_next;
return prev;
}
ListNode* reverseKGroup(ListNode* head, int k) {
if (head == nullptr || k == 1) {
return head;
}
ListNode sentinel;
sentinel.next = head;
auto prev_section = &sentinel, prev = head;
auto curr = head->next;
int count = 1;
auto begin_section = head;
while (curr != nullptr) {
auto nxt = curr->next;
if (count % k == k - 1) {
prev_section->next = reverse_section(begin_section, curr->next);
prev_section = begin_section;
begin_section = prev_section->next;
}
prev = curr;
curr = nxt;
count++;
}
return sentinel.next;
}
};