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


Please Help Members By Posting Answers For Below Questions

Can c++ be faster than c?

582


What is private, public and protected inheritance?

579


What is the benefit of c++?

581


Write a program to add three numbers in C++ utilizing classes.

609


How many static variables are created if you put one static member into a template class definition?

552






What are signs of manipulation?

575


Is c++ map a hash table?

557


What is static in c++?

582


What is a static element?

563


Can a class be static in c++?

557


What is flush programming?

556


How does c++ sort work?

548


What is the difference between new() and malloc()?

604


When we use Abstract Class and when we use Interface?where we will implement in real time?

1654


How do you define a class in c++?

632