Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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

How do we make a global variable accessible across files? Explain the extern keyword?

1848


Is c is a low level language?

1097


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

1030


write an algorithm to display a square matrix.

2670


How can I run c program?

1143


while loop contains parts a) initialisation, evalution of an expression,increment /decrement b) initialisation, increment/decrement c) condition evalution d) none of the above

1209


What does 3 mean in texting?

1061


Can a variable be both constant and volatile?

1074


Dont ansi function prototypes render lint obsolete?

1093


What is a pointer in c plus plus?

1267


What is a list in c?

1000


Explain union. What are its advantages?

1048


Tell me what are bitwise shift operators?

1114


What is the sizeof () a pointer?

944


Why void main is used in c?

1031