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
What does. int *x[](); means ?
Why #include is used in c language?
What is the best way to comment out a section of code that contains comments?
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
How can I call fortran?
Can we change the value of constant variable in c?
What is the significance of an algorithm to C programming?
What does %c do in c?
What is structure data type in c?
Tell me when would you use a pointer to a function?
How is a null pointer different from a dangling pointer?
What is the return type of sizeof?
What is pass by value in c?
my project name is adulteration of chille powder.how can i explain it to the hr when he asks me about the project?
What type of function is main ()?