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.

Implementation

  • 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.

Algorithm

  • deleteFirst()
  • IF Head == Null
    EXIT
  • 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();
        ll.addItem(60);
        ll.addItem(50);
        ll.addItem(40);
        ll.addItem(30);
        ll.addItem(20);
        ll.addItem(10);
        ll.display();
        ll.deletelast();  
        System.out.println("List after deletion: ");  
        ll.display();
        ll.deletelast();  
        System.out.println("List after deletion: ");  
        ll.display();
    }
}
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) {
            this.data = data;
            this.next = 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(temp.data + "–>");
            // Set temp to point to the next node
            temp = temp.next;
        }
        System.out.println("END");
    }
    // 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;
        }
        this.size++;
    }
    public void deletelast() {  
       if(head == null) {  
            System.out.println("List is empty");  
            return;  
        }  
        else if (head!=tail){   
            Node current = head;  
            while(current.next != tail) {  
                 current = current.next;  
                }  
            tail = current;  
            tail.next = null; 
            }  
        else {  
            head = tail = null;  
            }  
        }  
    } 
}
10–>20–>30–>40–>50–>60–>END
List after deletion: 
10->20–>30–>40–>50–>END