Interview Questions and Answers :: Cisco
Home > Experience Archives > Cisco > Interview Question Set > Discussion
1. How to find the nth node from last in single linklist.
Answer:
void GetNthFromLast(struct node* head, int n)
{
int len = n, i;
struct node *temp = *temp1 = head;
while (temp != NULL)
{
if (len > 0)
{
temp = temp->next;
len--;
continue;
}
temp = temp->next;
temp1 = temp1->next;
}
if (len)
{
printf("\r\n Node in the given Linked list is not enough \r\n");
return;
}
printf ("%d\n", temp1->data);
return;
}
Aditya Singh
4 Dec, 2018 4:26 PM
void GetNthFromLast(struct node* head, int n)
{
int len = n, i;
struct node *temp = *temp1 = head;
while (temp != NULL)
{
if (len > 0)
{
temp = temp->next;
len--;
continue;
}
temp = temp->next;
temp1 = temp1->next;
}
if (len)
{
printf("\r\n Node in the given Linked list is not enough \r\n");
return;
}
printf ("%d\n", temp1->data);
return;
}
{
int len = n, i;
struct node *temp = *temp1 = head;
while (temp != NULL)
{
if (len > 0)
{
temp = temp->next;
len--;
continue;
}
temp = temp->next;
temp1 = temp1->next;
}
if (len)
{
printf("\r\n Node in the given Linked list is not enough \r\n");
return;
}
printf ("%d\n", temp1->data);
return;
}
Report Error
Report Error
Please Login First Click Here