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
Can c++ be faster than c?
What is private, public and protected inheritance?
What is the benefit of c++?
Write a program to add three numbers in C++ utilizing classes.
How many static variables are created if you put one static member into a template class definition?
What are signs of manipulation?
Is c++ map a hash table?
What is static in c++?
What is a static element?
Can a class be static in c++?
What is flush programming?
How does c++ sort work?
What is the difference between new() and malloc()?
When we use Abstract Class and when we use Interface?where we will implement in real time?
How do you define a class in c++?