Print reverse of a linked list without actually reversing

JAVA Program to Print reverse of a linked list

In this page we’ll take a look at a JAVA program in which we’ll print reverse of a linked list without actually reversing it .We’ll simply have to traverse to last node and print node data and traverse back.

printing reverse of a linked list without actually reversing

Algorithm

In this program we are not actually reversing a Linked List, but just printing the Linked List backwards, for doing so we will be following the below steps -:

  • Step 1 – Take a pointer ptr.
  • Step 2 – Check whether the Linked List has nodes or not.
  • Step 3 – If the linked list is empty, return 0.
  • Step 4 – If the linked list has nodes, move the pointer ahead till it reaches NULL.
  • Step 5 – Backtrack and print the data of the nodes
JAVA Program to print reverse of a Linked List without actually reversing it

JAVA code to print reverse of a linked list without actually reversing it

Run

import java.util.*;
class LinkedList { // Head of list Node head; // Linked list Node class Node { int data; Node next; Node(int d) { data = d; next = null; } } // Function to print reverse of // linked list void printReverse(Node head) { if (head == null) return; // Print list of head node printReverse(head.next); // After everything else is printed, // Print head System.out.print(head.data + " "); } // 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(" "); } public void addFirst(int new_data) { Node new_node = new Node(new_data); new_node.next = head; head = new_node; } } public class Main{ public static void main(String args[]) { // Create linked LinkedList llist = new LinkedList(); llist.addFirst(42); llist.addFirst(13); llist.addFirst(25); llist.addFirst(10); System.out.println("Linked List before reversing"); llist.display(); System.out.println("Linked List after reversing"); llist.printReverse(llist.head); }
Linked List before reversing
10 25 13 42  
Linked List after reversing
42 13 25 10 

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Singly Linked List

  • Introduction to Linked List in Data Structure
    Click Here
  • Linked List in –
    C | C++ | Java
  • Singly Linked List in –
    C | C++ | Java
  • Insertion in singly Linked List –
    C | C++ | Java
  • Insertion at beginning in singly Linked List  –
    C | C++Java
  • Insertion at nth position in singly Linked List  –
    C | C++Java
  • Insertion at end in singly Linked List  –
    C | C++Java
  • Deletion in singly Linked List  –
    C | C++Java
  • Deletion from beginning in singly linked list :
    C | C++ | Java
  • Deletion from nth position in singly linked list :
    C | C++ | Java
  • Deletion from end in singly linked list :
    C | C++ | Java
  • Linked List Insertion and Deletion –
    C | C++Java
  • Reverse a linked list without changing links between nodes (Data reverse only) –
    C | C++Java
  • Reverse a linked list by changing links between nodes –
    C | C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Insertion in the middle Singly Linked List –
    C | C++Java
  • Insertion in a Sorted Linked List –
    C | C++Java
  • Delete alternate nodes of a Linked List –
    C | C++Java
  • Find middle of the linked list –
    C | C++Java
  • Reverse a linked list in groups of given size –
    C | C++Java
  • Find kth node from end of the linked list –
    C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list –
    C | C++Java
  • Check whether linked list is palindrome or not –
    C | C++Java
  • Fold a Linked List –
    C | C++Java
  • Insert at given Position –
    C | C++Java
  • Deletion at given Position –
    C | C++Java

Singly Linked List

  • Introduction to Linked List in Data Structure
  • Linked List in – C | C++ | Java
  • Singly Linked List in – C | C++ | Java
  • Insertion in singly Linked List – C | C++ | Java
    • Insertion at beginning in singly Linked List  – C | C++Java
    • Insertion at nth position in singly Linked List  – C | C++Java
    • Insertion at end in singly Linked List  – C | C++Java
  • Deletion in singly Linked List  – C | C++Java
    • Deletion from beginning in singly linked list : C | C++ | Java
    • Deletion from nth position in singly linked list : C | C++ | Java
    • Deletion from end in singly linked list : C | C++ | Java
  • Reverse a linked list without changing links between nodes (Data reverse only) – C | C++Java
  • Linked List Insertion and Deletion – C | C++Java
  • Reverse a linked list by changing links between nodes – C | C++Java
  • Linked List insertion in the middle – C | C++Java
  • Print reverse of a linked list without actually reversing – C |C++ | Java
  • Search an element in a linked list – C | C++Java
  • Insertion in a Sorted Linked List – C | C++Java
  • Delete alternate nodes of a Linked List – C | C++Java
  • Find middle of the linked list – C | C++Java
  • Reverse a linked list in groups of given size – C | C++Java
  • Find kth node from end of the linked list – C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list – C | C++Java
  • Check whether linked list is palindrome or not – C | C++Java
  • Fold a Linked List – C | C++Java
  • Insert at a given position – C | C++Java
  • Delete at a given position – C | C++Java