Google News
logo
Java program to delete a node from the beginning of the singly linked list
In the following example of Java program to delete a node from the beginning 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 the first node of the list
    public void deleteFirst() {
        if (head == null) {
            return;
        }
        head = head.next;
    }
    
    // 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.deleteFirst();
        System.out.println("\nThe linked list after deleting the first node is: ");
        list.display();
    }
    
}
Output :
Enter the number of nodes: 5
Enter the data for node 1: 5
Enter the data for node 2: 3
Enter the data for node 3: 7
Enter the data for node 4: 11
Enter the data for node 5: 16
The linked list is: 
5 3 7 11 16 
The linked list after deleting the first node is: 
3 7 11 16
The above 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 the first node 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 deleteFirst method, the head node is set to the second node in the list, effectively removing the first node.

In the display method, the list is traversed from the head node to the end, and each node's data is printed to the console.

In the main method, the user is prompted to enter the number of nodes to create, and the data for each node is entered. Nodes are added to the list using the add method, and the list is displayed using the display method. The first node is then deleted using the deleteFirst method, and the updated list is displayed using the display method.