Print a linked list java – Java Program to Print a Linked List in Reverse

Print a linked list java: In this article, we are going to see how we can print a linked list in reverse by using Java programming language.

Java Program to Print a Linked List in Reverse

As we know linked list is a type of liner data structure where the first element is called as the head and last element is called as the tail. Where the first element refers to the next element and so on, last element refers to null. Printing the linked list elements in reverse order means printing it from tail to head.

Let’s see a program to understand it more clearly.

Approach:

  • Create a linked list by creating an object of that class.
  • Add some elements to the list.
  • Display the elements.
  • Use a recursive method to print the list in reverse. It goes from the head to the tail and prints in reverse.

Program:

import java.util.*;
// Main class
public class Main
{
    // Class Node that defines the two linked list variables
    class Node
    {
        int data;
        Node nextNode;
        // constructor to create a node
        public Node(int data) 
        {    
            this.data = data;    
            this.nextNode = null;    
        }    
    }

    // Setting the head and end of the node as NULL  
    public Node head = null;    
    public Node tail = null;  
    // Print the reverse of the linked list
    public void reverse(Node head) 
    {
        if (head != null) 
        {
            // iterates until it reaches the tail i.e. null
            reverse(head.nextNode);
            // Print the data item
            System.out.print(head.data+",");
        }
    }
    // method to add a node to the linked list
    public void add(int data)
    {
        Node newNode = new Node(data);
        // Checks if there was any previous node
        //if the list is empty then head and tail both will point to newNode
        if(head==null)
        {
            head = newNode;
            tail = newNode;
        }
        else
        {
            tail.nextNode = newNode;
            tail = newNode;
        }
    }
    // Method to display all the nodes of the linked list
    public void show()
    {
        Node curr = head;
        // If the head is pointing to no node then the linked list is empty
        if(head==null)
            System.out.println("Linked List is empty");
        else
        {
            System.out.println("The nodes are:");
            while(curr!=null)
            {
                System.out.print(curr.data+",");
                curr = curr.nextNode;
            }
            System.out.println();
        }
    }
    
    //driver method
    public static void main(String[] args) 
    {
        // create an object of the main class
        Main ll = new Main();
        // adding elements to the linked list
        ll.add(10);
        ll.add(20);
        ll.add(30);
        ll.add(40);
        ll.add(50);
        // display the nodes
        ll.show();
        // Printing the list in reverse
        System.out.println("The reverse of the linked list is");
        ll.reverse(ll.head);
    }
}
Output:

The nodes are:
10,20,30,40,50,
The reverse of the linked list is
50,40,30,20,10,

Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.