- Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathreverse-nodes-in-k-group.cpp
More file actions
Latest commit
43 lines (43 loc) · 1.05 KB
/
Copy pathreverse-nodes-in-k-group.cpp
File metadata and controls
43 lines (43 loc) · 1.05 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
/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~
--||author : codechaser||--
~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
classSolution
{
public:
ListNode *reverseKGroup(ListNode *head, int k)
{
ListNode *prev = NULL;
ListNode *curr = head;
ListNode *next;
int count = 0;
ListNode *temp = head;
int c = 0;
while (c < k && temp != NULL)
{
temp = temp->next;
c++;
}
if (c < k)
return curr;
while (curr != NULL && count < k)
{
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
count++;
}
if (next != NULL)
head->next = reverseKGroup(next, k);
return prev;
}
};
/*
|---------------------------------------------------|
||| https://codeforces.com/profile/codechaser |||
||| https://www.codechef.com/users/codechaser |||
||| https://github.com/code-chaser |||
|---------------------------------------------------|
*/