Doubt- getting run error linked lists

I have run almost the same code, in kth node from end, but still getting runtime error

please look into it!

#include <iostream>
using namespace std;

struct node{
    int data;
    struct node* next;
};
struct node* head1=NULL;
struct node* head2=NULL;

void insert_end(int data,struct node** head){
    struct 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;
    }
}

//merge two sorted nodes
node* middle(struct node* head){
    struct node* slow=head;
    struct node* fast=head->next;
    if(head==NULL || head->next==NULL){
        return head;
    }
    while(fast!=NULL && fast->next!=NULL){
        fast=fast->next->next;
        slow=slow->next;
    }
    return slow;
}

node* merge(struct node* a,struct node* b){
    if(a == NULL){
        return b;
    }
    else if(b == NULL){
        return a;
    }
    struct node* c;
    if(a->data <= b->data){
        c=a;
        c->next=merge(a->next,b);
    }
    else{
        c=b;
        c->next=merge(a,b->next);
    }
    return c;
}

void display(struct node* head){
    struct node* temp=head;
    while(temp!= NULL){
        cout<<temp->data<<" ";
        temp=temp->next;
    }
    cout<<endl;
}
void check(struct node* head1,int k){
    if(head1==NULL || head1->next==NULL){
        cout<<head1->data<<endl;
    }
    else{
    struct node* slow=head1;
    struct node* fast=head1;
    int count=0;
    while(count<k){
        count++;
        fast=fast->next;
    }
    while(fast!=NULL){
        fast=fast->next;
        slow=slow->next;
    }
    cout<<slow->data<<endl;
}
}
int main() {
    //insert in beginning
    int t;
    cin>>t;
    while(t--){
    head1=NULL;
    
    int data;
    cin>>data;
    while(data!=-1){
        insert_end(data,&head1);
        cin>>data;
    }
    int k;
    cin>>k;
    check(head1,k);
    }
    return 0;
}