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 / vivek

ListNodePtr* tenthListNodePtr = NULL; //Holds 10th last node

ListNodePtr* tempListNodePtr = firstNode;

int counter = 1;

//Advance the tempListNodePtr to 10th node from first //node.
while( (counter < 10) && (tempListNodePtr) )
{

tempListNodePtr = tempListNodePtr->nextNode;

++counter;
}

tenthListNodePtr = firstNode;

//Advance both the pointers. also check for cycle.
// Since two ptrs differ by 10, when last node is reached
//the result will be there.
while( (tempListNodePtr) && (tempListNodePtr != firstNode) )
{

tenthListNodePtr = tenthListNodePtr->nextNode;

tempListNodePtr = tempListNodePtr->nextNode;

}

Is This Answer Correct ?    14 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is time_t c++?

791


What is c++ 11 and c++ 14?

807


What you know about structures in C++?

788


Explain the term memory alignment?

889


Can we overload operator in c++?

752






What are the comments in c++?

751


What is null c++?

807


Can a Structure contain a Pointer to itself?

782


What are features of c++?

819


How do I write a c++ program?

784


What are advantages of c++?

766


Does c++ have a hash table?

723


What is a c++ class?

835


Write some differences between an external iterator and an internal iterator? Describe the advantage of an external iterator.

795


Differentiate between an inspector and a mutator ?

897