How to remove a given key from the Singly Linked List in Java ?

By | June 21, 2020

In the previous three posts, We Learned How to remove/delete first and last Node from Singly Linked List.
We also learned how to remove Linked list Node from a given position or Index.

In this post, We will learn How to remove a given key from the Singly Linked List in Java?

Program Logic:

  1. if position == 0 then move head node to next node (head = head.next) and return true
  2. We have to traverse the list from the head to the last node and compare each node value with the supplied key if found then delete that Node using Step-3 and return true else return false.
  3. We have to set a pointer to the node previous to the node to be deleted. So we have to run a loop till the last node and get a pointer to the previous node.

Complete Source code :

Output of this program :

Original LinkedList:
10 20 30 40 50 60
After removing an Element = 40 LinkedList is:
10 20 30 50 60

You May Also Like:

That’s all about the How to remove a given key from the Singly Linked List in Java?
If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published.