-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path25.reverse-nodes-in-k-group.cpp
More file actions
88 lines (76 loc) · 2.03 KB
/
25.reverse-nodes-in-k-group.cpp
File metadata and controls
88 lines (76 loc) · 2.03 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
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
#include "testharness.h"
#include <cassert>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#include <functional>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k) {
if (head == NULL || k < 2) return head;
ListNode dummy(0);
ListNode* last_tail = &dummy;
ListNode* left_nodes = head;
while (left_nodes != NULL) {
ListNode* new_tail = left_nodes;
last_tail->next = reverseFirstK(&left_nodes, k);
last_tail = new_tail;
}
return dummy.next;
}
ListNode* reverseFirstK(ListNode** left_nodes, int k) {
assert(k > 1);
assert(*left_nodes != NULL);
// first we check whether we can reverse
int n = 0;
ListNode* tmp = *left_nodes;
while (tmp != NULL) {
if (++n == k) break;
tmp = tmp->next;
}
if (n < k) {
tmp = *left_nodes;
*left_nodes = NULL;
return tmp;
}
// do reverse
ListNode dummy(0);
ListNode* list_end = &dummy;
ListNode* current = *left_nodes;
while (k-- > 0 && current != NULL) {
ListNode* current_next = current->next;
current->next = list_end->next;
list_end->next = current;
current = current_next;
}
*left_nodes = current;
return dummy.next;
}
};
TEST(Solution, test) {
{
ListNode a1(1);
ListNode b1(2);
ListNode c1(3);
ListNode d1(4);
ListNode e1(5);
a1.next = &b1;
b1.next = &c1;
c1.next = &d1;
d1.next = &e1;
ListNode* head = &a1;
ListNode* end = NULL;
ListNode* current = reverseKGroup (&a1, 3);
while (current != NULL) {
std::cout << current->val << std::endl;
current = current->next;
}
}
}