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


Please Help Members By Posting Answers For Below Questions

What does. int *x[](); means ?

639


Why #include is used in c language?

602


What is the best way to comment out a section of code that contains comments?

784


an expression contains relational operators, assignment operators, and arithmatic operstors. In the absence of parentheses, they will be evaluated in which of the following order a) assignment, relational, arithematic b) arithematic, relational, assignment c) relational, arithematic, assignment d) assignment, arithematic, relational

812


How can I call fortran?

644






Can we change the value of constant variable in c?

579


What is the significance of an algorithm to C programming?

597


What does %c do in c?

587


What is structure data type in c?

574


Tell me when would you use a pointer to a function?

610


How is a null pointer different from a dangling pointer?

558


What is the return type of sizeof?

596


What is pass by value in c?

597


my project name is adulteration of chille powder.how can i explain it to the hr when he asks me about the project?

1122


What type of function is main ()?

590