How to remove a node from a linked list java – Java Program to Remove Nodes from the Beginning and End of a Linked List

How to remove a node from a linked list java: In this article we are going to see how we can remove nodes from the beginning and end of a linked list by using Java programming language.

Java Program to Remove Nodes from the Beginning and End of a Linked List

As you know each element is called as a node in Linked List where the first node is called head and last node is called tail. Here we have to remove both head and tail from the Linked List.

Approach:

  • Create a linked list by creating an object of that class.
  • Add some elements to the list.
  • Display the list.
  • Delete the node at the beginning of the list by calling the user defined method deleteBeg() method. This method points the head to the second element or both head and tail to null (if no other elements are there) and removes the first node from the list.
  • Display the list.
  • Now to delete a node from the end use the user defined method deleteEnd( ) method. It removes the element at the end and points the tail to the second last element or null(if no other elements are there)
  • Display the 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;  
    // Delete a node from the beginning of a linked list
    public void deleteBeg() 
    {
        if(head == null) 
        {  
            System.out.println("Linked list is empty");
        }
        else 
        {  
            //checks if there are more than one element in the list
            // If there is then it points to second element
            // or else points to null
            if(head != tail) 
            {  
                head = head.nextNode;  
            }
            else 
            {  
                head = tail = null;  
            }  
        }  
    }

    // Delete a node from the end of a linked list
    public void deleteEnd() 
    {  
        //Checks if the list is empty  
        if(head == null) 
        {  
            System.out.println("Linked list is empty");
        }  
        else 
        {  
            //Checks whether the list contains only one element  
            if(head != tail ) 
            {  
                Node curr = head;  
                //Iterate to the second last element
                while(curr.nextNode != tail) 
                {  
                    curr = curr.nextNode;  
                }  
                //Point the tail to the second last element
                tail = curr;  
                tail.nextNode = null;  
            }  
            //If there is no other element point tail to null
            else 
            {  
                head = tail = null;  
            }  
        }  
    }
    
    // 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();
        System.out.println("Deleting a node from the beginning...");
        ll.deleteBeg();
        ll.show();
        System.out.println("Deleting a node from the end...");
        ll.deleteEnd();
        ll.show();
    }
}
Output:

The nodes are:
10,20,30,40,50,60,70,80,90,100,
Deleting a node from the beginning...
The nodes are:
20,30,40,50,60,70,80,90,100,
Deleting a node from the end...
The nodes are:
20,30,40,50,60,70,80,90,

Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.