Find Nth Node from End of the Singly linked list.
 

Que 4. Find Nth Node from End of the Singly linked list ? Answers.....

Logic:

1. Create two pointer's pointing at start of the list.

2. Move only First pointer up to Nth Node from start.

3. Now start moving both pointers until the First pointer reaches the end.

4. The node to which second pointer is pointing is the Nth from end of the linked list.

C -Implementation.

int FindNthFromEnd(unsigned int index, Node **head)
{
Node *current = *head;
Node *nth= *head;
unsigned int j=0;
for(; (( current != NULL) &&( j < n)); j++, current= current->next){}

if(current == NULL ) {
printf("%dnt element does not exist", n);
return -1;
}
else {

while(current != NULL) {

nth = nth->next;
current = current->next;
}
return nth->data;
}
}

Back to question's page...