Java Program to Remove a Node from a Given Position in a Linked List

In this article we are going to see how we can remove a node from a given position in a linked list by using Java programming language.

Java Program to Remove a Node from a Given Position in a Linked List

Each element in Java is treated as a node in Linked List. Each position is like index which starts from 0 and goes on. To delete a specific node from given position, you have to specify the index.

Let’s see a program to understand it more clearly.

Approach:

  • Create a linked list and add some elements.
  • Display the elements to the user.
  • Ask the user to enter a position to delete a node.
  • Pass it to the user function. The function removes the node at the position and makes the previous node point to the next node in the list.
  • Display the new linked list.

Program:

import java.util.*;
// Main class
public class Main
{
    // Class Node that defines the two linked list variables
    class Node
    {
        int data;
        Node nextNode;
        // constructor to create a node
        public Node(int data) 
        {    
            this.data = data;    
            this.nextNode = null;    
        }    
    }

    // Setting the head and end of the node as NULL  
    public Node head = null;    
    public Node tail = null;  

    // Method to delete a node from a specified position from a linked list
    public void deletePos(int pos)
    {
        System.out.println("Removing node from position "+pos+"...");
        // If linked list is empty
        if (head == null)
        {
            System.out.println("The linked list is empty");
            return;
        }
        // Store head node
        Node temp = head;
        // If pos = 0, head shall be removed
        if (pos == 0) 
        {
            // Point head to the next node
            head = temp.nextNode;
            return;
        }
     
        //Iterate to the previous node of the to be deleted node
        for (int i = 0; temp != null && i < pos - 1;i++)
        temp = temp.nextNode;
     
        // If position is more than number of nodes
        if (temp == null || temp.nextNode == null)
        {
            System.out.println("Wrong input");
            return;
        }
     
        // Store the address of the next node
        Node nextNode = temp.nextNode.nextNode;
        // Unlink the node at the position
        temp.nextNode= nextNode;
    }

    // method to add a node to the linked list
    public void add(int data)
    {
        Node newNode = new Node(data);
        // Checks if there was any previous node
        if(head==null)
        {
            head = newNode;
            tail = newNode;
        }
        else
        {
            tail.nextNode = newNode;
            tail = newNode;
        }
    }
    
    // Method to display all the nodes of the linked list
    public void show()
    {
        Node curr = head;
        // If the head is pointing to no node then the linked list is empty
        if(head==null)
            System.out.println("Linked List is empty");
        else
        {
            System.out.println("The nodes are:");
            while(curr!=null)
            {
                System.out.print(curr.data+",");
                curr = curr.nextNode;
            }
            System.out.println();
        }
    }
    
    public static void main(String[] args) 
    {
        // create an object of the main class
        Main ll = new Main();
        // add elements to the linked list
        ll.add(10);
        ll.add(20);
        ll.add(30);
        ll.add(40);
        ll.add(50);
        ll.add(60);
        ll.add(70);
        ll.add(80);
        ll.add(90);
        ll.add(100);
        // display the nodes
        ll.show();
        // Taking user input
        System.out.println("Enter a position to delete a node");
        Scanner sc = new Scanner(System.in);
        int pos = sc.nextInt();
        // Deleting the node
        ll.deletePos(pos);
        // display the nodes
        ll.show();
        // Taking user input
        System.out.println("Enter another position to delete a node");
        pos = sc.nextInt();
        // Deleting the node
        ll.deletePos(pos);
        // display the nodes
        ll.show();
    }
}
Output:

The nodes are:
10,20,30,40,50,60,70,80,90,100,
Enter a position to delete a node
1
Removing node from position 1...
The nodes are:
10,30,40,50,60,70,80,90,100,
Enter another position to delete a node
3
Removing node from position 3...
The nodes are:
10,30,40,60,70,80,90,100,

Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.