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
Explain storage qualifiers in c++.
Is there a sort function in c++?
What is protected inheritance?
What is the difference between object-oriented programming and procedural programming?
What is a type library?
What is #include c++?
What is an inline function in c++?
What is input operator in c++?
which of the following is not an secondary constant a) array b) real c) union
What is the difference between set and map in c++?
What is the use of string in c++?
what does the following statement mean? int (*a)[4]
What is the difference between structure and class?
Differentiate between declaration and definition.
Do class declarations end with a semicolon?