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 oops in c++?
Explain friend class?
why and when we can declar member fuction as a private in the class?
Which sort does c++ use?
What is the difference between a type-specific template friend class and a general template friend class?
What are the uses of c++ in the real world?
What is meant by iomanip in c++?
Why do we use double in c++?
What is #include cstdlib in c++?
How do you print a string on the printer?
Why do we need runtime polymorphism in c++?
What is abstract class in c++?
What would happen on forgetting [], while deallocating an array through new?
What is namespace & why it is used in c++?
What is the fastest c++ compiler?