Code:
void nth_last_elment(NODE *ln, int N)
{
NODE *nelement = ln, *node = ln;int i = 0 ;
while(i < N && node != NULL){
i++;
node = node->next;
}
if(i != N)
{
printf("\n Lesser than N elements in list %d \n ",N);
return ;
}
else
{
while(node != NULL)
{
node = node->next;
nelement = nelement->next;
}
}
printf("\n %dth element of link list from end is : %d " ,N,nelement->info);
}
Love your blog and post. This page is a good reference for this question too:
ReplyDeletehttp://www.programmerinterview.com/index.php/data-structures/find-nth-to-last-element-in-a-linked-list/