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
What is type of 'this' pointer? Explain when it is get created?
Explain how to initialize a const data member.
Write a struct time where integer m, h, s are its members?
Ask to write virtual base class code?
What is virtual function? Explain with an example
Is c++ built on c?
What is switch case in c++ syntax?
What is ctime c++?
What methods can be overridden in java?
What kind of jobs can I get with c++?
Explain abstraction.
What is a NULL Macro? What is the difference between a NULL Pointer and a NULL Macro?
Why c++ is so important?
What type of question are asked in GE code writing test based on c++ data structures and pointers?
What do you mean by function overriding & function overloading in c++?