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 does obj stand for?
What is the difference between strcpy() and strncpy()?
List down the guideline that should be followed while using friend function.
What is a singleton c++?
What is virtual base class?
What is stoi in c++?
What is pointer to member?
What does asterisk mean in c++?
What are multiple inheritances (virtual inheritance)? What are its advantages and disadvantages?
What is a constant reference?
What is the limitation of cin while taking input for character array?
What is a stack? How it can be implemented?
Which programming language should I learn first?
Can we distribute function templates and class templates in object libraries?
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.