Linked list kth node runtime error

   Getting run error in kth node, please help me resolve it.

#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;
}

take care of those cases when k>= length of the linked list…just add a condition k=k%length of linked list at the beginning of shift function …