How to find 3rd element from last in a Linked List in one Iteration or How to find nth element from last in a Linked List.
use 2 pointers.
// let's assume the list has 3 or more than 3 elements.
current = head;
last3rd = head;
length = 0;
while ( current != null)
{
length++;
current = current.next();
if (length >3)
last3rd = last3d.next();
}
No comments:
Post a Comment