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

why and when we can declar member fuction as a private in the class?

1844


Differentiate between C and C++.

920


Define namespace in c++?

853


What language does google use?

881


What is the full form of dos?

793


Why do we need templates?

764


Do you know what are static and dynamic type checking?

845


Can you pass an array to a function in c++?

775


Is it possible to get the source code back from binary file?

1033


What does getch() do according to the ANSI C++ standard a) Reads in a character b) Checks the keyboard buffer c) Nothing in particular (Its not defined there)

810


What is the use of 'this' pointer?

1038


What is the full form of ios?

774


What are all predefined data types in c++?

815


Do you know about latest advancements in C++ ?

876


What do you mean by const correctness?

853