reverse-nodes-in-k-group
1.0.0
Reverse Nodes in k-Group
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1
#include <bits/stdc++.h>
2
3
using namespace
std;
4
8
struct
ListNode
9
{
10
int
val
;
11
ListNode
*
next
;
12
ListNode
() :
val
(0),
next
(nullptr) {}
13
ListNode
(
int
x) :
val
(x),
next
(nullptr) {}
14
ListNode
(
int
x,
ListNode
*
next
) :
val
(x),
next
(
next
) {}
15
};
16
17
class
Solution
18
{
19
public
:
20
ListNode
*
reverseKGroup
(
ListNode
* head,
int
k)
21
{
22
if
(!head || k == 1)
return
head;
23
24
// new list to reconstruct the reversed k-groups
25
list<ListNode*> new_list;
26
27
// k-sized stack for reversing groups
28
stack<ListNode*> s;
29
30
ListNode
* ptr = head;
31
bool
stop =
false
;
32
while
(1)
33
{
34
// skipping k times to establish if full k-sized group is present
35
ListNode
* fast = ptr;
36
for
(
int
i = 0; i < k; ++i)
37
{
38
if
(fast ==
nullptr
)
39
{
40
stop =
true
;
41
break
;
42
}
43
fast = fast->
next
;
44
}
45
if
(stop)
46
break
;
47
48
// pushing k-sized group to stack
49
for
(
int
i = 0; i < k; ++i)
50
{
51
s.push(ptr);
52
ptr = ptr->
next
;
53
}
54
55
// emptying stack into new list
56
while
(!s.empty())
57
{
58
new_list.push_back(s.top());
59
s.pop();
60
}
61
}
62
63
// append the rest normally
64
while
(ptr !=
nullptr
)
65
{
66
new_list.push_back(ptr);
67
ptr = ptr->
next
;
68
}
69
70
// reconstruct the list from head
71
head = new_list.front();
72
new_list.pop_front();
73
ptr = head;
74
for
(
ListNode
* node: new_list)
75
{
76
ptr->
next
= node;
77
ptr = ptr->
next
;
78
}
79
ptr->
next
=
nullptr
;
80
return
head;
81
}
82
};
83
84
void
print_tree
(
ListNode
* head)
85
{
86
ListNode
* ptr = head;
87
while
(ptr !=
nullptr
)
88
{
89
cout << ptr->
val
<<
' '
;
90
ptr = ptr->
next
;
91
}
92
cout <<
'\n'
;
93
}
94
95
void
delete_tree
(
ListNode
* head)
96
{
97
stack<ListNode*> s;
98
ListNode
* ptr = head;
99
while
(ptr !=
nullptr
)
100
{
101
s.push(ptr);
102
ptr = ptr->
next
;
103
}
104
while
(!s.empty())
105
{
106
ptr = s.top();
107
s.pop();
108
delete
ptr;
109
}
110
}
111
112
int
main
()
113
{
114
ListNode
* head =
new
ListNode
(1,
new
ListNode
(2,
new
ListNode
(3,
new
ListNode
(4,
new
ListNode
(5)))));
115
print_tree
(head);
116
head =
Solution
().
reverseKGroup
(head, 2);
117
print_tree
(head);
118
delete_tree
(head);
119
return
0;
120
}
Solution
Definition
main.cpp:18
Solution::reverseKGroup
ListNode * reverseKGroup(ListNode *head, int k)
Definition
main.cpp:20
print_tree
void print_tree(ListNode *head)
Definition
main.cpp:84
delete_tree
void delete_tree(ListNode *head)
Definition
main.cpp:95
main
int main()
Definition
main.cpp:112
ListNode
Definition for singly-linked list.
Definition
main.cpp:9
ListNode::val
int val
Definition
main.cpp:10
ListNode::ListNode
ListNode(int x)
Definition
main.cpp:13
ListNode::ListNode
ListNode()
Definition
main.cpp:12
ListNode::next
ListNode * next
Definition
main.cpp:11
ListNode::ListNode
ListNode(int x, ListNode *next)
Definition
main.cpp:14
main.cpp
Generated by
1.9.8