WHY this piece of code only pass 2 test cases and remaining shows TLE

Why it only pass only 2 test cases and shows a TLE
#include
using namespace std;

class node{
public:
int data;
node * next;
node(int d){
data = d;
next = NULL;
}
};

void CreateLinkList(node *& head , int num){
if(head == NULL){
node * n = new node(num);
head = n;
}
else{
node * temp = head;
while(temp->next != NULL){
temp = temp->next;
}
temp->next = new node(num);
}
}
void jump_position(node *& fast , int position){
while(position–){
fast = fast->next;
}
}
node * find_element_last(node * head , int position){
node * fast = head;
node * slow = head;

jump_position(fast , position);
while(fast->next != NULL){
	fast = fast->next;
	slow = slow->next;
}
return slow;		

}
node * last_element(node * head){
node * tail = head;
while(tail->next!=NULL){
tail = tail->next;
}
return tail;
}
void print(node * head){
while(head != NULL){
cout<data<<" ";
head = head->next;
}
}
int main(){
node * head = NULL;
int n;
cin>>n;
while(n–){
int a;
cin>>a;
CreateLinkList(head , a);
}

int k;
cin>>k;

node * prevK =  find_element_last(head , k);
node * kth = prevK->next;
node *  tail = last_element(head);

prevK->next = NULL;
tail->next = head;
head = kth;


print(head);


return 0;

}

@rajsaxena.personal you are not taking in account when k>n, to handle the cases where k>n, make k=k%n.
If this resolves your doubt mark it as resolved.

after making k=k%n. then also it not work

@rajsaxena.personal sorry for a bit late reply. But the code works on applying k=k%n.
Here is the modified code.


#include <bits/stdc++.h>

using namespace std;
class node{
public:
int data;
node * next;
node(int d){
data = d;
next = NULL;
}
};

void CreateLinkList(node *& head , int num){
if(head == NULL){
node * n = new node(num);
head = n;
}
else{
node * temp = head;
while(temp->next != NULL){
temp = temp->next;
}
temp->next = new node(num);
}
}
void jump_position(node *& fast , int position){
    
while(position--){
fast = fast->next;
}

}
node * find_element_last(node * head , int position){
node * fast = head;
node * slow = head;

jump_position(fast , position);

while(fast->next != NULL){
    fast = fast->next;
    slow = slow->next;
}
return slow;        
}
node * last_element(node * head){
node * tail = head;
while(tail->next!=NULL){
tail = tail->next;
}
return tail;
}
void print(node * head){
while(head != NULL){
cout<<head->data<<" ";
head = head->next;
}
}
int main(){
   
node * head = NULL;
int n;
cin>>n;
int N=n;
while(n--){
int a;
cin>>a;
CreateLinkList(head , a);
}


int k;
cin>>k;

k=k%N;
if(k!=0)
{node * prevK =  find_element_last(head , k);
node * kth = prevK->next;
node *  tail = last_element(head);
prevK->next = NULL;
tail->next = head;
head = kth;
}


print(head);


return 0;
}

If this resolves your doubt mark it as resolved.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.