If you want to remove an item from the linked list in Java by key, you should follow several steps. The fundamental idea to do with the pointers: the front pointer and the back pointer. Set the minimum interval between them, which is one element. Move both of them with the help of the “while loop” until the front pointer meets the needed value element.
Remove an item in the Linked List—an example of the implementation in Java by key.
To remove the element in the linked list, you have to follow several steps:
- You should create two pointers. The front pointer starts from the head of the list. The front pointer’s primary purpose is to stop the loop when pointing to the item with the needed value. The back pointer starts as a null. The reason for that is that it is no assigned to any element yet. The primary purpose of the back pointer is to connect the previous element with the element after the current;
//Put out selection to the start of the list
ListNode frontPointer = head;
//Temporary node to track item to be attached with
ListNode backPointer = null;
//The value we are looking for
int searchedValue = 34;
- Start “while loop.” The primary purpose of that loop is to find the right place for a new element in the linked list. It will loop until the front pointer is not null and its value is equal to the needed value. That means if the front pointer’s value is the same, we are looking for. We can stop looping.
//In case first item is item we are looking for we just remove it by moving head to the next item
if (frontPointer != null && frontPointer.data == searchedValue) {
head = frontPointer.next;
return;
}
//We are going through the loop until we find the item with needed value and then go out
while (frontPointer != null && frontPointer.data != searchedValue) {
//Moving temporary item to next if item not found yet
backPointer = frontPointer;
//Moving selected item to compare searched key with data of the item
frontPointer = frontPointer.next;
}
- Remove the element. After the loop stops, our front and back pointers suppose to be in the right place. We can connect the back pointer element to the next of the front pointer. As a result, we will remove the needed element and restructure the linked list.
//We have found the item with the need value and connecting temporary item to the next item after selected
//So removing selected item between them
backPointer.next = frontPointer.next;
//Going back to the head to log the list
frontPointer = head;
- Set our current element to the first element of the list. So we can loop through the list and check the result.
Detailed schema of inserting the element
Full code solution example.
private void removeTheItemFromTheListByKey() {
//You need to create linked list first
creatingLinkedList();
//Put out selection to the start of the list
ListNode frontPointer = head;
//Temporary node to track item to be attached with
ListNode backPointer = null;
//The value we are looking for
int searchedValue = 122;
//In case first item is item we are looking for we just remove it by moving head to the next item
if (frontPointer != null && frontPointer.data == searchedValue) {
head = frontPointer.next;
return;
}
//We are going through the loop until we find the item with needed value and then go out
while (frontPointer != null && frontPointer.data != searchedValue) {
//Moving temporary item to next if item not found yet
backPointer = frontPointer;
//Moving selected item to compare searched key with data of the item
frontPointer = frontPointer.next;
}
//We have found the item with the need value and connecting temporary item to the next item after selected
//So removing selected item between them
backPointer.next = frontPointer.next;
//Going back to the head to log the list
frontPointer = head;
while (frontPointer != null) {
//Showing data of each item in the logs
Log.d("linkedList", "-> " + frontPointer.data);
frontPointer = frontPointer.next;
}
Log.d("linkedList", "-> " + null);
}
In this article, you figured out the easiest way to remove an element from the linked list java by key.
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.
- Find the nth element from the last in Linked List (Java)
- Removing duplicates from the linked list
- Inserting an item in the sorted Linked list in Java
Leave Any Questions Related To This Article Below!