How to remove an item from the Linked List in Java by key. – IT Blog
IT Blog
Boris IT Expert

How to remove an item from the Linked List in Java by key.

Boris~November 2, 2020 /Linked List

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:

//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;
//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;

Detailed schema of inserting the element

remove an item from the Linked List in Java

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:

Leave Any Questions Related To This Article Below!