Java Program to Delete a Node from a Linked List

Introduction

Deleting a node from a linked list is a fundamental operation that involves removing a specific node and adjusting the pointers of the surrounding nodes to maintain the list’s structure. This guide will walk you through writing a Java program that deletes a node from a singly linked list.

Problem Statement

Create a Java program that:

  • Implements a singly linked list.
  • Deletes a specified node from the linked list.
  • Displays the linked list before and after deleting the node.

Example:

  • Input: Linked list: 1 -> 2 -> 3 -> 4 -> 5, delete node with value 3

  • Output: 1 -> 2 -> 4 -> 5

  • Input: Linked list: 10 -> 20 -> 30 -> 40, delete node with value 20

  • Output: 10 -> 30 -> 40

Solution Steps

  1. Create the Linked List and Node Structure: Define a Node class to represent each element in the linked list and a LinkedList class to manage the list.
  2. Add Nodes to the Linked List: Implement methods to add nodes to the linked list.
  3. Delete a Node from the Linked List:
  • Traverse the linked list to find the node with the specified value.
  • Adjust the next pointer of the previous node to skip the node to be deleted.
  1. Display the Result: Output the linked list before and after deleting the node.

Java Program

// Java Program to Delete a Node from a Linked List // Author: https://www.rameshfadatare.com/ class Node { int data; Node next; // Constructor to initialize the node public Node(int data) { this.data = data; this.next = null; } } class LinkedList { Node head; // Method to add a new node at the end of the list public void add(int data) { Node newNode = new Node(data); if (head == null) { // Step 1: Initialize the head if the list is empty head = newNode; } else { // Step 2: Traverse to the end of the list and add the new node Node current = head; while (current.next != null) { current = current.next; } current.next = newNode; } } // Method to delete a node with the specified value public void deleteNode(int value) { if (head == null) return; // Step 3: If the list is empty, return // Step 4: If the head node itself holds the value to be deleted if (head.data == value) { head = head.next; // Move the head to the next node return; } // Step 5: Traverse the list to find the node to delete Node current = head; Node prev = null; while (current != null && current.data != value) { prev = current; current = current.next; } // Step 6: If the value was not found in the list, return if (current == null) return; // Step 7: Skip the node to delete it prev.next = current.next; } // Method to display the linked list public void display() { Node current = head; while (current != null) { System.out.print(current.data + " -> "); current = current.next; } System.out.println("null"); } } public class DeleteNodeFromLinkedList { public static void main(String[] args) { LinkedList list = new LinkedList(); // Adding elements to the linked list list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); System.out.println("Original Linked List:"); list.display(); // Deleting a node from the linked list list.deleteNode(3); System.out.println("Linked List after deleting node with value 3:"); list.display(); } } 

Explanation

Step 1: Initialize the Node Class

  • The Node class represents a single node in the linked list. Each node contains data and a reference to the next node in the list.
  • The constructor initializes the node with data and sets the next pointer to null.

Step 2: Initialize the LinkedList Class

  • The LinkedList class manages the linked list. The class contains the head node that points to the first node in the list.
  • The add() method appends a new node to the end of the list. If the list is empty, the head node is set to the new node.

Step 3: Delete a Node from the Linked List

  • The deleteNode() method removes a node with the specified value from the linked list:
    • If the head node contains the value to be deleted, the head is updated to point to the next node.
    • If the value is found in a non-head node, the previous node’s next pointer is updated to skip the node containing the value.
    • If the value is not found in the list, the method returns without making changes.

Output Example

Original Linked List: 1 -> 2 -> 3 -> 4 -> 5 -> null Linked List after deleting node with value 3: 1 -> 2 -> 4 -> 5 -> null 

Example with Different Input

If you modify the input list to:

list.add(10); list.add(20); list.add(30); list.add(40); 

And delete the node with value 20:

list.deleteNode(20); 

The output will be:

Original Linked List: 10 -> 20 -> 30 -> 40 -> null Linked List after deleting node with value 20: 10 -> 30 -> 40 -> null 

Conclusion

This Java program demonstrates how to delete a node from a singly linked list by adjusting the pointers of the surrounding nodes. The program covers essential concepts such as linked list manipulation, node traversal, and pointer management. This exercise is valuable for understanding how to efficiently manage linked lists and perform deletion operations in Java programming.

Leave a Comment

Scroll to Top