Deletion from the end in a Singly Linked List in JAVA

JAVA Program for Deletion from end in a linked list

JAVA Program for deletion from the end of a Singly Linked List in JAVA

Compilation of Data Elements of similar types with the address to the next element is termed as a linked list. To perform a deletion in a linked list at the end we need to change the address in the pointer location of the second last node to NULL. Deletion in a linked list is nothing but redirection of pointers from one address reference to other.


  • For the Deletion of a node from the end of a Linked List first We’ve to check that the list is not empty, if yes then exit.
  • Now the pointer from the second last node will be redirected towards the Null pointer at the end of any Linked List.
  • If there is only one node in the linked list then the head and the tail both will point to the null.


  • deleteFirst()
  • IF Head == Null
  • ELSE IF Head != Tail
    Head = Head -> Next
  • ELSE
    Head = Tail = Null
JAVA Program to delete a node from the end of a Linked List

Code in JAVA Programming Language

import java.util.*;
public class Main 
    public static void main(String[] args) throws Exception {
        LinkedList ll = new LinkedList();
        System.out.println("List after deletion: ");  
        System.out.println("List after deletion: ");  
class LinkedList {
    private class Node {
        int data;
        Node next;
        // Node constructor
        // There are two fields in the node- data and address of next node
        public Node(int data, Node next) {
   = data;
   = next;
    private Node head;
    private Node tail;
    private int size;
    // Linked list constructor
    public LinkedList() {
            this.head = null;
            this.tail = null;
            this.size = 0;
    // Function to traverse and print the linked list
    public void display() {
        Node temp = head;
        while (temp != null) {
            System.out.print( + "–>");
            // Set temp to point to the next node
            temp =;
    // Function to add a node in beginning of linked list
    public void addItem(int item) {
        // Create a temp node which points to head
        Node temp = new Node(item, head);
        // If linked list is empty, temp is the head and tail node both
        if (this.size == 0) {
            this.head = this.tail = temp;
        // else set the head such that it now points to temp node
        else {
            this.head = temp;
    public void deletelast() {  
       if(head == null) {  
            System.out.println("List is empty");  
        else if (head!=tail){   
            Node current = head;  
            while( != tail) {  
                 current =;  
            tail = current;  
   = null; 
        else {  
            head = tail = null;  
List after deletion: