How to resolve MLE error here?

My code is running for most test cases and my own test cases but for some predefined test cases it is giving MLE error. Can you help resolving it?

/#include
using namespace std;

class node{
public:
int data;
node* next;
node* prev;

node(int d){
    data = d;
    next = NULL;
	prev = NULL;
}

};

void InsertAtTail(node*&head, int d){
if(head == NULL){
head = new node(d);
return;
}
node* tail=head;
while(tail->next!=NULL){
tail = tail->next;
}
tail->next = new node(d);
return;
}

node* take_input(int n){
int d;
node*head = NULL;
int count=0;
while(count<n){
cin>>d;
InsertAtTail(head,d);
count++;
}
return head;
}

node* append(node*&head,int k){
node* fast = head;

node* slow = head;

for (int i = 0; i < k; i++) {

    fast = fast->next;
}

while (fast->next != NULL) {

    slow = slow->next;

    fast = fast->next;
}

fast->next = head;

head = slow->next;

slow->next = NULL;

return head;

}

void print(node*head){
while(head!=NULL){
cout<data<<" ";
head = head->next;
}
cout<<endl;
}

int main() {
int n;
cin>>n;
node* head = take_input(n);
int k;
cin>>k;
node* newList = append(head,k);
print(newList);
return 0;
}

hi @itiikhare,
try this
5
1 2 3 4 5
5

do
k = k%n;
after taking input

thank you! It works. But can u explain why it is needed?

@itiikhare it will point to null pointer, which u r using subsequently