Follow Our FB Page << CircleMedia.in >> for Daily Laughter. We Post Funny, Viral, Comedy Videos, Memes, Vines...

Memory is not a constraint. In a single iteration(NOTE: you
can't go back), how will you find out the 10th last

Answers were Sorted based on User's Feedback

Memory is not a constraint. In a single iteration(NOTE: you can't go back), how will you find ..

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

Memory is not a constraint. In a single iteration(NOTE: you can't go back), how will you find ..

The approach is to have 2 ptrs ,ptr1 and ptr2.Where ptr1
would start at the first node of the linked list and ptr2
would be at the kth position from ptr1.In this case k=10.

From there on until ptr2->next!=null keep moving ptr1 and
ptr2 by one each.When ptr2 reached the last element of the
linked list.Ptr1 would be pointing to the 10th (kth) last
element.

Example:

1->2->3->4->5->6->7->8->9->10->11->12. In this case 10th
last element would be 3.

Now till ptr2->next!=null ptr1=ptr1->next and ptr2=ptr2->next.

This way when ptr2 reaches 12 ptr1 would be at 3,which is
the 10th last element.

 Is This Answer Correct ? 8 Yes 0 No

Memory is not a constraint. In a single iteration(NOTE: you can't go back), how will you find ..

suppose list is already created having some elements
poninted by start ......
node* start;
node*p[10];
node* ptr;
int n=0;
ptr=start;
while(ptr->next==null)
{
p[(n++)%10]=ptr;

}
if(n<10) printf("there r unsufficient elements");
else { ptr=p[n%10]}

 Is This Answer Correct ? 6 Yes 1 No

Memory is not a constraint. In a single iteration(NOTE: you can't go back), how will you find ..

Ashutosh,

 Is This Answer Correct ? 6 Yes 2 No

Memory is not a constraint. In a single iteration(NOTE: you can't go back), how will you find ..

ahutosh ,the kid is right pos% 10 will do fine,,are u
nuts,work on basic maths

 Is This Answer Correct ? 6 Yes 2 No

Memory is not a constraint. In a single iteration(NOTE: you can't go back), how will you find ..

simple,

in case the list has say 90 elements, then for every 10
elements, he is writing to the array. though the array size
is 10, this is done by saying pos%10.
thus when the list is iterated he just substracts 10 from
the pos and then does %10 since he has to factor in, that
array starts from 0 and not from 1 :)

 Is This Answer Correct ? 5 Yes 2 No

Memory is not a constraint. In a single iteration(NOTE: you can't go back), how will you find ..

NO, it won't work.

because, after the wile loop, pos is pointing to the last
element NOT 10th last. So, you have to go back 10 elements.

 Is This Answer Correct ? 5 Yes 2 No

Memory is not a constraint. In a single iteration(NOTE: you can't go back), how will you find ..

i saw it after posting ... :)
correction in while loop ...
while(ptr!=null)
{
p[(n++)%10]=ptr;
ptr=ptr->next;
}
................................

 Is This Answer Correct ? 3 Yes 0 No

Memory is not a constraint. In a single iteration(NOTE: you can't go back), how will you find ..

it can be just done tis way!!
temp=first;
{
}
printf("%d",temp->data);

 Is This Answer Correct ? 2 Yes 0 No

Memory is not a constraint. In a single iteration(NOTE: you can't go back), how will you find ..

I got it, thank you.

but one suggestion, instead of using

printf("Tenth last element is %d",nodes[(pos-10)%10]->data);

you can simply give,
printf("Tenth last element is %d",nodes[pos%10]->data);

 Is This Answer Correct ? 3 Yes 2 No

More C++ General Interview Questions

What is a conversion constructor?

What are c++ stream classes?

What are the differences between a struct and a class in C++?

difference between c and c++?

What is a template in c++?

When is a template a better solution than a base class?

Differentiate between late binding and early binding. What are the advantages of early binding?

Differentiate between the message and method?

implement stack using stack.h headerfile functions

What is an incomplete type in c++?

Can a list of string be stored within a two dimensional array?

How does list r; differs from list r();?

Categories