Google News
logo
Java program to delete a node from the end of the singly linked list
In the following example of Java program to delete a node from the end 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 end of the list
    public void deleteEnd() {
        if (head == null) {
            return;
        }
        if (head.next == null) {
            head = null;
            return;
        }
        Node current = head;
        while (current.next.next != null) {
            current = current.next;
        }
        current.next = null;
    }
    
    // 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();
        list.deleteEnd();
        System.out.println("\nThe linked list after deleting the last node is: ");
        list.display();
    }
    
}
Output :
Enter the number of nodes: 5
Enter the data for node 1: 12
Enter the data for node 2: 19
Enter the data for node 3: 26
Enter the data for node 4: 34
Enter the data for node 5: 42
The linked list is: 
12 19 26 34 42 
The linked list after deleting the last node is: 
12 19 26 34 
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 end 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 deleteEnd method, the method checks if the list is empty or has only one node. If the list is empty, the method simply returns. If the list has only one node, the head is updated to null. Otherwise, the method traverses the list until it reaches the second to last node, and updates its next node to null, effectively removing the last node.

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

In the main method, the program prompts the user to enter the number of nodes and the data for each node, and adds them to the list. The program then displays the list, deletes the last node, and displays the updated list.