forked from vinay-kumar99/Hactoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathK_reverse.cpp
More file actions
110 lines (77 loc) · 1.67 KB
/
Copy pathK_reverse.cpp
File metadata and controls
110 lines (77 loc) · 1.67 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include<iostream>
using namespace std;
#include "Node.cpp"
// Hard Problem
class pair_ret{
public:
Node *head;
Node *tail;
};
pair_ret reverse(Node *head){
if(head==NULL || head->next==NULL){
pair_ret ans;
ans.head=head;
ans.tail=head;
return ans;
}
pair_ret x = reverse(head->next);
x.tail->next=head;
x.tail = x.tail->next;
head->next=NULL;
return x;
}
Node * krever(Node * head, int k){
if(head==NULL || head->next==NULL){
return head;
}
Node * temp=head;
int count=0;
while(temp->next!=NULL && count<k-1){
count++;
temp=temp->next;
}
//Node * str = temp->next;
Node * x = krever(temp->next,k);
Node * temp1=head;
int count1=0;
while(temp1->next!=NULL && count1<k-1){
count1++;
temp1=temp1->next;
}
temp1->next=NULL;
pair_ret small_ll = reverse(head);
small_ll.tail->next=x;
return small_ll.head;
}
Node * take_input(){
int data;
cin>>data;
Node* head = NULL;
Node*tail=NULL;
while(data!=-1){
Node *newnode = new Node(data);
if(head==NULL){
head=newnode;
tail=newnode;
}
else{
tail->next=newnode;
tail=tail->next;
}
cin>>data;
}
return head;
}
void print(Node*head){
while(head!=NULL){
cout<<head->data<<" ";
head=head->next;
}
}
int main(){
Node * head = take_input();
int n;
cin>>n;
head = krever(head,n);
print(head);
}