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 malloc in c++?
What is a storage class?
Why c++ is the best language?
Is swift a good first language?
In which situation the program terminates before reaching the breakpoint set by the user at the beginning of the mainq method?
What is basic if statement syntax?
What is operator overloading in c++ example?
What is the use of :: operator in c++?
Is string an object in c++?
How do I write a c++ program?
What are pointer-to-members in C++? Give their syntax.
What is isdigit c++?
Define Virtual function in C++.
Which format specifier is used for printing a pointer value?
What is the purpose of template?