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 are the types of array in c++?

0 Answers  


What is c++ hiding?

0 Answers  


When should you use multiple inheritance?

2 Answers  


Explain the pure virtual functions?

0 Answers  


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

0 Answers  






plz send me National informatics center paper pattern

1 Answers  


Difference between a homogeneous and a heterogeneous container

0 Answers  


Explain the difference between static and dynamic binding of functions?

0 Answers  


sir i want to study the c++ course but ino what is the qualification and the study methode please reply more details in c++

1 Answers   NIIT,


What are c++ variables?

0 Answers  


What are pointer-to-members in C++? Give their syntax.

0 Answers   HAL,


What do you mean by early binding?

0 Answers  


Categories