Implement a function that returns the 5th element from the
end in a singly linked list of integers in one pass.
Answer Posted / abhijit annaldas
Sorry, it was my mistake.. previous answer was not correct.
Here is the corrected one...
node* getNthFromLast(node* head, int n)
{
int c=0;
node *nth=head;
node *pt=head;
while(pt!=NULL)
{
pt=pt->next;
c++;
//if c=n then nth node is already set to head.
if(c>n)
nth=nth->next;
}
if(c<n) //LL contains less than n nodes
return (*node)0;
else
return *nth;
}
Use it as..
fifth_node = getNthFromLast(head, 5);
| Is This Answer Correct ? | 3 Yes | 0 No |
Post New Answer View All Answers
How many types of functions are there in c?
Describe the order of precedence with regards to operators in C.
how to solve "unable to open stdio.h and conio.h header files in windows 7 by using Dos-box software
What is the value of c?
Explain null pointer.
What is null in c?
What is the method to save data in stack data structure type?
what are the program that using a two dimensional array that list the odd numbers and even numbers separately in a given 10 inputs values
What is your stream meaning?
What type of function is main ()?
diff between exptected result and requirement?
What is static function in c?
What are header files why are they important?
Explain low-order bytes.
What is a char c?