-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverseKgrps.cpp
More file actions
63 lines (52 loc) · 1.05 KB
/
reverseKgrps.cpp
File metadata and controls
63 lines (52 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<iostream>
using namespace std;
class node{
public:
int data;
node* next;
node(int d){
this -> data = d;
this -> next = NULL;
}
};
node* Kreverselist(node* head, int k){
if(head == NULL){
return NULL;
}
// reversing list
node* curr = head;
node* prev = NULL;
node* forward = NULL;
int cnt = 0;
while(curr != NULL && cnt < k){
forward = curr -> next;
curr -> next = prev;
prev = curr;
curr = forward;
cnt++;
}
if(forward != NULL){
head -> next = Kreverselist(forward , k);
}
return prev;
}
void print(node* head){
while(head != NULL){
cout << head -> data <<" ";
head = head -> next ;
}
cout << endl;
}
int main(){
node* head = new node(1);
node* n2 = new node(2);
head -> next = n2;
node* n3 = new node(3);
n2 -> next = n3;
node* n4 = new node(4);
n3 -> next = n4;
print(head);
head= Kreverselist(head , 2);
print(head);
return 0;
}