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
Can a void pointer point to a function?
What is c variable?
What is linear search?
my project name is adulteration of chille powder.how can i explain it to the hr when he asks me about the project?
What is local and global variable in c?
What functions are used for dynamic memory allocation in c language?
What is the difference between a string and an array?
how many errors in c explain deply
Is c a great language, or what?
how to write a c program to print list of fruits in alpabetical order?
Is it better to bitshift a value than to multiply by 2?
Is that possible to store 32768 in an int data type variable?
Is null always equal to 0(zero)?
find the output? void r(int a[],int c, int n) { if(c>n) { a[c]=a[c]+c; r(a,++c,n); r(a,++c,n); } } int main() { int i,a[5]={0}; r(a,0,5); for(i=0;i<5;i++) printf("\n %d",a[i]); getch(); }
What are the types of pointers in c?