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 many types of functions are there in c?

1134


Describe the order of precedence with regards to operators in C.

1047


how to solve "unable to open stdio.h and conio.h header files in windows 7 by using Dos-box software

3309


What is the value of c?

1061


Explain null pointer.

1105


What is null in c?

1083


What is the method to save data in stack data structure type?

1080


what are the program that using a two dimensional array that list the odd numbers and even numbers separately in a given 10 inputs values

1690


What is your stream meaning?

1226


What type of function is main ()?

1038


diff between exptected result and requirement?

2047


What is static function in c?

1111


What are header files why are they important?

1085


Explain low-order bytes.

1028


What is a char c?

1041