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 type of 'this' pointer? Explain when it is get created?

580


Explain how to initialize a const data member.

589


Write a struct time where integer m, h, s are its members?

511


Ask to write virtual base class code?

2146


What is virtual function? Explain with an example

581






Is c++ built on c?

556


What is switch case in c++ syntax?

615


What is ctime c++?

566


What methods can be overridden in java?

664


What kind of jobs can I get with c++?

589


Explain abstraction.

609


What is a NULL Macro? What is the difference between a NULL Pointer and a NULL Macro?

608


Why c++ is so important?

600


What type of question are asked in GE code writing test based on c++ data structures and pointers?

3501


What do you mean by function overriding & function overloading in c++?

596