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...

Memory is not a constraint. In a single iteration(NOTE: you
can't go back), how will you find out the 10th last
node/item in a linked list.

Answer Posted / ranjani

The approach is to have 2 ptrs ,ptr1 and ptr2.Where ptr1
would start at the first node of the linked list and ptr2
would be at the kth position from ptr1.In this case k=10.

From there on until ptr2->next!=null keep moving ptr1 and
ptr2 by one each.When ptr2 reached the last element of the
linked list.Ptr1 would be pointing to the 10th (kth) last
element.

Example:

1->2->3->4->5->6->7->8->9->10->11->12. In this case 10th
last element would be 3.

Start with ptr1 at node 1 and ptr2 at node 10.
Now till ptr2->next!=null ptr1=ptr1->next and ptr2=ptr2->next.

This way when ptr2 reaches 12 ptr1 would be at 3,which is
the 10th last element.

Is This Answer Correct ?    8 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is stream and its types in c++?

1104


Explain the difference between using macro and inline functions?

1183


What is difference between c++ and c ++ 14?

1112


How c functions prevents rework and therefore saves the programers time as wel as length of the code ?

1131


Why do we use structure in c++?

1132


Const char *p , char const *p What is the difference between the above two?

1256


What are c++ templates used for?

1177


What is a hashmap c++?

1158


Is c++ a high level language?

1053


What is the difference between the parameter to a template and the parameter to a function?

1172


What is the c++ programming language used for?

1095


Is it possible to provide default values while overloading a binary operator?

1324


What is a template in c++?

1970


What are advantages of c++?

1057


What function initalizes variables in a class: a) Destructor b) Constitutor c) Constructor

1207