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 time_t c++?
What is c++ 11 and c++ 14?
What you know about structures in C++?
Explain the term memory alignment?
Can we overload operator in c++?
What are the comments in c++?
What is null c++?
Can a Structure contain a Pointer to itself?
What are features of c++?
How do I write a c++ program?
What are advantages of c++?
Does c++ have a hash table?
What is a c++ class?
Write some differences between an external iterator and an internal iterator? Describe the advantage of an external iterator.
Differentiate between an inspector and a mutator ?