Google News
logo
Java program to delete a node from the middle of the singly linked list
In the following example of Java program to delete a node from the middle of a singly linked list :
Program :
import java.util.Scanner;

public class SinglyLinkedList {
    
    // Node class
    static class Node {
        int data;
        Node next;
        
        Node(int data) {
            this.data = data;
            this.next = null;
        }
    }
    
    // Head of the linked list
    Node head;
    
    // Method to add a node to the end of the list
    public void add(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
        } else {
            Node current = head;
            while (current.next != null) {
                current = current.next;
            }
            current.next = newNode;
        }
    }
    
    // Method to delete a node from the middle of the list
    public void deleteMiddle(int position) {
        if (head == null) {
            return;
        }
        Node current = head;
        if (position == 1) {
            head = current.next;
            return;
        }
        for (int i = 1; current != null && i < position - 1; i++) {
            current = current.next;
        }
        if (current == null || current.next == null) {
            return;
        }
        current.next = current.next.next;
    }
    
    // Method to display the list
    public void display() {
        Node current = head;
        while (current != null) {
            System.out.print(current.data + " ");
            current = current.next;
        }
    }
    
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        SinglyLinkedList list = new SinglyLinkedList();
        System.out.print("Enter the number of nodes: ");
        int n = input.nextInt();
        for (int i = 1; i <= n; i++) {
            System.out.print("Enter the data for node " + i + ": ");
            int data = input.nextInt();
            list.add(data);
        }
        System.out.println("The linked list is: ");
        list.display();
        System.out.print("\nEnter the position of the node to delete: ");
        int position = input.nextInt();
        list.deleteMiddle(position);
        System.out.println("\nThe linked list after deleting the node at position " + position + " is: ");
        list.display();
    }
    
}
Output :
Enter the number of nodes: 5
Enter the data for node 1: 4
Enter the data for node 2: 9
Enter the data for node 3: 6
Enter the data for node 4: 10
Enter the data for node 5: 19
The linked list is: 4 9 6 10 19 
Enter the position of the node to delete: 4
The linked list after deleting the node at position 4 is: 4 9 6 19 
This program defines a Node class to represent a node in the linked list. Each node contains an integer data and a reference to the next node in the list. The SinglyLinkedList class contains a reference to the head of the list, methods to add nodes to the end of the list, delete a node from the middle of the list, and display the list.

In the add method, a new node is created with the specified data, and added to the end of the list. If the list is empty, the new node becomes the head. If the list is not empty, the method traverses the list until it reaches the last node, and adds the new node as its next node.

In the deleteMiddle method, the method accepts a position to delete a node from. If the position is the head node, the head is updated to the next node. Otherwise, the method traverses the list to find the node before the node to be deleted.

If the node to be deleted is not found or is the last node, the method simply returns. Otherwise, the next node of the node before the node to be deleted is updated to the next-next node, effectively removing the node to be deleted.

In the display method, the method traverses the list and prints each node's data..