-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathLinked List K Reverse.cpp
95 lines (81 loc) · 1.83 KB
/
Linked List K Reverse.cpp
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
/* Hacker Blocks */
/* Title - Linked List K Reverse */
/* Created By - Akash Modak */
/* Date - 2/8/2020 */
// Given a head to Linked List L, write a function to reverse the list taking k elements at a time. Assume k is a factor of the size of List.
// You need not have to create a new list. Just reverse the old one using head.
// Input Format
// The first line contains 2 space separated integers N and K
// The next line contains N space separated integral elements of the list.
// Constraints
// 0 <= N <= 10^6 0 <= K <= 10^6
// Output Format
// Display the linkedlist after reversing every k elements
// Sample Input
// 9 3
// 9 4 1 7 8 3 2 6 5
// Sample Output
// 1 4 9 3 8 7 5 6 2
// Explanation
// N = 9 & K = 3
// Original LL is : 9 -> 4 -> 1 -> 7 -> 8 -> 3 -> 2 -> 6 -> 5
// After k Reverse : 1 -> 4 -> 9 -> 3 -> 8 -> 7 -> 5 -> 6 -> 2
#include<iostream>
using namespace std;
class Node
{
public:
int data;
Node* next;
Node();
Node(int data){
this->data=data;
this->next=NULL;
}
~Node();
};
void buildlist(Node* &head,int data){
if(head==NULL){
Node *temp=new Node(data);
head = temp;
return;
}
Node* temp = head;
while(temp->next!=NULL)
temp=temp->next;
temp->next = new Node(data);
return;
}
Node* rev(Node* head,int k){
Node* current = head;
Node* prev = NULL;
Node* next = NULL;
int count = 0;
while(current!= NULL and count<k){
next = current->next;
current->next = prev;
prev = current;
current = next;
count++;
}
if(next!=NULL)
head->next = rev(next,k);
return prev;
}
int main() {
int n,k;
cin>>n>>k;
Node* head = NULL;
for(int i=0;i<n;i++){
int temp;
cin>>temp;
buildlist(head,temp);
}
head = rev(head,k);
Node* c = head;
while(c!=NULL){
cout<<c->data<<" ";
c=c->next;
}
return 0;
}