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 do we make a global variable accessible across files? Explain the extern keyword?
Is c is a low level language?
Tell me when would you use a pointer to a function?
write an algorithm to display a square matrix.
How can I run c program?
while loop contains parts a) initialisation, evalution of an expression,increment /decrement b) initialisation, increment/decrement c) condition evalution d) none of the above
What does 3 mean in texting?
Can a variable be both constant and volatile?
Dont ansi function prototypes render lint obsolete?
What is a pointer in c plus plus?
What is a list in c?
Explain union. What are its advantages?
Tell me what are bitwise shift operators?
What is the sizeof () a pointer?
Why void main is used in c?