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
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 |
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 |
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 |
Answer / mms zubeir
Ashutosh,
Can you explain how your answer sync with the question?
Is This Answer Correct ? | 6 Yes | 2 No |
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 |
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 |
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 |
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 |
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 |
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 |
Implement stack operations with pointers with appropriate exception checks.
Please explain the reference variable in c++?
Write a program to add three numbers in C++ utilizing classes.
differance between copy & clon
What are the benefits of c++?
What is split a string in c++?
Define stacks. Provide an example where they are useful.
Which is the best c++ compiler for beginners?
Describe delete operator?
what is data Abstraction
How can you prevent accessing of the private parts of my class by other programmers (violating encapsulation)?
Can a program run without main function?