please help me remove the runtime errors, I think logically my code is working fine!
#include
using namespace std;
class node{
public:
int data;
node* next;
};
node* head=NULL;
void insert_end(int data,node* &head){
node* temp=new node;
temp->data=data;
temp->next=NULL;
if(head == NULL){
head=temp;
return;
}
else{
node* temp1=head;
while(temp1->next!=NULL){
temp1=temp1->next;
}
temp1->next=temp;
}
}
void display(node* head){
node* temp=new node;
temp=head;
while(temp!= NULL){
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}
void check(node* head,int k){
if(head==NULL || head->next==NULL){
cout<<head->data<<endl;
}
else{
node* slow=head;
node* fast=head;
int count=0;
while(count<k){
count++;
fast=fast->next;
}
while(fast!=NULL){
fast=fast->next;
slow=slow->next;
}
cout<<slow->data<<endl;
}
}
node* shift(node* head,int k){
node* slow=head;
node* fast=head;
int count=0;
while(count<k){
count++;
fast=fast->next;
}
while(fast->next!=NULL){
fast=fast->next;
slow=slow->next;
}
fast->next=head;
head=slow->next;
slow->next=NULL;
//cout<<head->data<<" "<<fast->next->data<<endl;
return head;
}
int main() {
//insert in beginning
int n;
cin>>n;
int data;
for(int i=0;i<n;i++){
cin>>data;
insert_end(data,head);
}
int k;
cin>>k;
//head=reverse_k(head,k);
head=shift(head,k);
display(head);
//check(head,k);
return 0;
}