How to remove a node from a Singly Linked List at a given position in Java?

By | June 21, 2020

In a previous couple of posts, We Learned How to remove/delete first and the last Node from Singly Linked List.

In this post, We will learn How to remove/delete a node from a Singly Linked List at a given position in Java?

Program Logic:

  1. if position == 0 then move head node to next node (head = head.next)
  2. if position < 0 Or position > = number of nodes in Linked List then throws an Exception
  3. We have to set a pointer to the node previous to the node to be deleted. So if the position is not zero then we have to run a loop position-1 times and get a pointer to the previous node.

Output of this program:

Original LinkedList:
10 20 30 40 50 60
After removing an Element from given position = 2 LinkedList is:
10 20 40 50 60

You May Also Like:

That’s all about the How to delete a node from a Singly Linked List at a given position 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.