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 is type of 'this' pointer? Explain when it is get created?
What are references in c++? What is a local reference?
What is the difference between map and hashmap in c++?
Is set c++?
Is swift better than c++?
What are the benefits of operator overloading?
How const int *ourpointer differs from int const *ourpointer?
What is else syntax in c++?
Can I run c program in turbo c++?
What is data binding in c++?
Perform addition, multiplication, subtraction of 2-D array using Operator Overloading.
Write a program to find the Factorial of a number
Explain pass by value and pass by reference.
What is a character in c++?
Can we define a constructor as virtual in c++?