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 are static type checking?
What is diamond problem in c++?
What is constant in c++ with example?
What are the uses of static class data?
What do you understand by zombie objects in c++?
Specify some guidelines that should be followed while overloading operators?
What is #include iostream?
In int main(int argc, char *argv[]) what is argv[0] a) The first argument passed into the program b) The program name c) You can't define main like that
What does override mean in c++?
Who was the creator of c++?
What is polymorphism & list its types in c++?
Differentiate between the message and method in c++?
Write a program using shift_half( ) function to shift the elements of first half array to second half and vice versa.
Who made c++?
What is endl?