Time limit exceeded

Ankit, plz change your buildlist function as :

void buildlist(node *&head)
{
int N;
cin>>N;
int data;
while(N>0)
{
cin>>data;
insertAtTail(head,data);
Nā€“;
}
}

and then use the separate function for inserting nodes at the end of linked list.

How this will help to avoid TLE?

This is so because for large cases, the list is taking large time to build in your code thts why ā€¦