One of the common questions on the technical interview is to find the nth element from the last in the linked list in Java. To solve this problem, you should use the pointers’ technique. We should set two-pointers and set a needed distance between them. After that, we should move both of them until the front pointer comes to the null element.
How to find the nth element from the last in the Linked list. The detailed explanation.
Here are the detailed steps you have to follow:
- Set up as a variable the number of the element we are looking for. For instance, we need the second element from the last in the linked list (Java). Our variable would have a value equals to 2.
//Item we are looking for
int neededItem = 2;
- Set up the front pointer and the counter. With the loop’s help, we need to set the front pointer in the right place in the list. As a result, the distance between the front pointer and the back pointer will be equal to 2
//Pointer which helps in finding needed item from begin of the list
ListNode frontPointer = head;
//Moving pointer from beginning of the list to needed number of items
while (counter < neededItem) {
frontPointer = frontPointer.next;
counter++;
}
- Move the front and the back pointers. The front pointer’s primary purpose is to point to the end of the list, which is a null element, and stop the loop. The back pointer’s goal is to move after the front pointer with the needed distance, and when the front pointer stops, the second will point to the required element in the list. Since we need the second element, the distance between pointers supposes to be two elements. After the front pointer stops, the back pointer will point to the required element.
//Pointer which helps in finding needed item based on referencePointer value
ListNode backPointer = head;
//Moving both pointers untill reference become null mean list is over
while (frontPointer != null) {
frontPointer = frontPointer.next;
backPointer = backPointer.next;
}
The detailed scheme:
The full implementation code (Java).
private void findThirdNodeFromTheEnd() {
//You need to create linked list first
creatingLinkedList();
//Pointer which helps in finding needed item from begin of the list
ListNode frontPointer = head;
//Pointer which helps in finding needed item based on referencePointer value
ListNode backPointer = head;
//Item we are looking for
int neededItem = 2;
//Counter of the items
int counter = 0;
//Moving pointer from beginning of the list to needed number of items
while (counter < neededItem) {
frontPointer = frontPointer.next;
counter++;
}
//Moving both pointers untill reference become null mean list is over
while (frontPointer != null) {
frontPointer = frontPointer.next;
backPointer = backPointer.next;
}
Log.d("linkedList", "The item number " + neededItem + " from the end has value " + backPointer.data);
}
In this article, you figured out the easiest way how to find the nth element from the last in the linked list java.
Full code and more examples in my facebook group.
Useful links:
- Search for an element in the Linked List in Java
- Deleting an item in the Linked List in Java (Android Studio)
- Adding items to the Linked List in Java.
- Getting the length (size) of Linked List in Java (Android Studio)
- Two ways to implement a Linked List in Java. Example.
- Reversing the linked list.
- Middle of the linked list example.
Leave Any Questions Related To This Article Below!