Make Selenium Easy

4. Data Structure Programs – Delete Node From Unsorted LinkedList In Java

Introduction

As a part of the Data structure for Testers series, in this post, we will learn to write a Java program to delete a node from a Linked List.

Prerequisite post

Data Structure For Testers – Implement Linked List In Java

Did you know that I have started a YouTube channel as well and I need your support to make it successful. Please do watch content then comment, like, share, and obviously subscribe.

Logic to delete a node from a Linked List

To delete a node from a LinkedList we need to find it first in the list. As the given Linked List is not sorted we need to traverse the list from head to the tail node. To delete the node in a Linked list we just need to skip the reference as shown below:-

Before deleting a node

After deleting a node with data 2

If I need to delete a node with data as 2 from the list then the just previous node ( i.e. data as 3) of a node to be deleted needs to point to the next node (i.e. data as 10) of the node to be deleted.

Now we need to think of all edge and other scenarios.

Step by step logic

To traverse through a linked list we need to have a reference to the head of the linked list.

What if the head node itself is the node to be deleted? We should check it and if it is a match then just make a new head next to the current head.

If the head node is not the node to be deleted then we need to start traversing the linked list. We need to keep traversing till the end of the list or if we find the node to be deleted. We need to have another node reference to store the previous node as we need to jump/skip the reference as shown in the above pic.

Now we can have two scenarios:- a. if the node to be deleted is not found in the linked list – If the node to be deleted is not found in the linked list then the current node will point to null after traversing. In this case, we need not do anything and simply return the existing list.

b. if the node to be deleted is found in the linked list then we just need to shift the next of prev reference to next of current as shown in the above image. Please note here if the node to be deleted is found in the list then reference “cur” will point to the node to be deleted.

Are we missing something? Yes! Edge cases. What if the list is empty? We just need to think of edge scenarios and handle the program as it is highly expected in interviews.

Complete Program

Node.java
LinkedList.java
DeleteNodeFromLinkedList.java
Output

You can download/clone the above sample project from here.

You can subscribe to my YouTube channel RetargetCommon to learn from video tutorials.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning

Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.

Many other topics you can navigate through the menu

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.