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 / 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 |
Post New Answer View All Answers
What is a constant reference?
Why is polymorphism useful?
Define basic type of variable used for a different condition in C++?
What is a list c++?
What are manipulators used for?
Explain the scope of resolution operator.
Can we use this pointer in a class specific, operator-overloading function for new operator?
Write a C/C++ program that connects to a MySQL server and checks if the InnoDB plug-in is installed on it. If so, your program should print the maximum number of concurrent threads that the InnoDB plug-in can create
What is the difference between global int and static int declaration?
What do nonglobal variables default to a) auto b) register c) static
Please explain the reference variable in c++?
What is fixed in c++?
What are built-in functions? What is the syntax for the definition?
Describe about storage allocation and scope of global, extern, static, local and register variables?
Explain what is class definition in c++ ?