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
node/item in a linked list.

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 ..

Answer / 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

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

Answer / ranjani

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.

Start with ptr1 at node 1 and ptr2 at node 10.
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 ..

Answer / atul bodke

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 ..

Answer / mms zubeir

Ashutosh,

Can you explain how your answer sync with the question?

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 ..

Answer / dg

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 ..

Answer / ravindra

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 ..

Answer / ashutosh

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 ..

Answer / atul bodke

correction in prev answer ...
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 ..

Answer / rohith

it can be just done tis way!!
temp=first;
while(temp->link->link->link->link->link->link->link->link-
>link!=NULL)
{
temp=temp->link;
}
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 ..

Answer / mms zubeir

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

Post New Answer



More C++ General Interview Questions

What is a conversion constructor?

1 Answers  


What are c++ stream classes?

0 Answers  


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

6 Answers   Amazon, Wipro,


difference between c and c++?

38 Answers   Cognizant, IBM, Infosys, Oracle, Sarva Shiksha Abhiyan, Wipro,


What is a template in c++?

0 Answers  






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

1 Answers  


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

0 Answers  


Differentiate between the message and method?

1 Answers  


implement stack using stack.h headerfile functions

1 Answers   Exilant, GMG, Subex, University,


What is an incomplete type in c++?

0 Answers  


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

0 Answers  


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

0 Answers  






Categories