getting time limit exceeded error… how to improve it ??
#include
using namespace std;
class Node{
public:
int data;
Node *next;
};
void push(Node *&head,int data){
Node *n =new Node;
n->data =data;
n->next=head;
head=n;
}
Node *reverse (Node *head,int k){
Node *current=head;
Node *next=NULL;
Node *prev=NULL;
int count =0;
while(count < k && current !=NULL){
next = current->next;
current->next=prev;
prev = current;
current=next;
}
if(next != NULL){
head->next= reverse(next,k);
}
return prev;
}
void print(Node *head){
while(head!=NULL){
cout<data;
head=head->next;
}
}
int main(){
Node *head =NULL;
int n,k;
cin>>n>>k;
while(n>0){
int data;
cin>>data;
push(head,data);
}
head= reverse(head,k);
print(head);
return 0;
}