Google News
logo
Java program to create a singly linked list of n nodes and display it in reverse order
In the following example of Java program to create a singly linked list of n nodes and display it in reverse order :
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 display the list in reverse order
    public void displayReverse() {
        if (head == null) {
            return;
        }
        Node current = head;
        Node prev = null;
        Node next = null;
        while (current != null) {
            next = current.next;
            current.next = prev;
            prev = current;
            current = next;
        }
        head = prev;
        display();
    }
    
    // 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();
        System.out.println("\nThe linked list in reverse order is: ");
        list.displayReverse();
    }
    
}
Output :
Enter the number of nodes: 5
Enter the data for node 1: 3
Enter the data for node 2: 1
Enter the data for node 3: 6
Enter the data for node 4: 9
Enter the data for node 5: 15
The linked list is: 
3 1 6 9 15 
The linked list in reverse order is: 15 9 6 1 3 
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, display the list in reverse order, 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 displayReverse method, the list is traversed from the head node to the end, and each node is reversed to point to its previous node instead of the next one. The head node becomes the last node, and the last node becomes the head node. The display method is then called to display the list in its new order.

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 list is then displayed in reverse order using the displayReverse method.