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

What does obj stand for?

845


What is the difference between strcpy() and strncpy()?

843


List down the guideline that should be followed while using friend function.

822


What is a singleton c++?

781


What is virtual base class?

761


What is stoi in c++?

922


What is pointer to member?

776


What does asterisk mean in c++?

830


What are multiple inheritances (virtual inheritance)? What are its advantages and disadvantages?

802


What is a constant reference?

827


What is the limitation of cin while taking input for character array?

1728


What is a stack? How it can be implemented?

914


Which programming language should I learn first?

783


Can we distribute function templates and class templates in object libraries?

800


I was a c++ code and was asked to find out the bug in that. The bug was that he declared an object locally in a function and tried to return the pointer to that object. Since the object is local to the function, it no more exists after returning from the function. The pointer, therefore, is invalid outside.

824