C++ program to print reverse a linked list without actually reversing

C++ program to reverse a linked list without actually reversing it

How to print reverse of a linked list without actually reversing in CPP programming?

In this article, we will learn to write a C++ program to print reverse of a linked list without actually reversing. For doing this we will create a function that will recursively print data from last node till the beginning node without actually reversing it. Further in this article we will see steps and algorithm for the same.

Steps write a C++ program to print reverse of a linked list without actually reversing

These steps are used to print reverse of a linked list without actually reversing in CPP programming
  1. Start the program and initialize the variables.
  2. Create function to create list (listBanao).
  3. The above created function will help us to create linked list.
  4. Now create another function toto print reverse of a linked list without actually reversing (reverse).
  5. This function will have our main algorithm to reverse the given linked list.
  6. This function will help to display the linked list in reverse order without actually reversing it.

Syntax

class Node  

    int data; 
    Node *next; 
}; 
C++ Program to print reverse of a Linked List without actually reversing it

Algorithm to print reverse of a linked list without actually reversing in C++

Below is the algorithm used to print reverse of a linked list without actually reversing in C++
  • VOID REVERSE(NODE *STNODE)
  • IF (STNODE == NULL)
  • RETURN
  • REVERSE(STNODE->NEXTPTR)
  • PRINT STNODE->NUM

 

Program to print reverse of a linked list without actually reversing in C++

Run
#include<iostream>
using namespace std;

struct node
{
  int num;
  node *nextptr;
} *stnode;			//node constructed

void listBanao (int n);
void reverse (node * stnode);

int main ()
{
  int n, num, item;

  cout << "Enter the number of nodes: ";
  cin >> n;
  listBanao (n);
  cout << "After reversing\n";
  reverse (stnode);

  return 0;
}

void listBanao (int n)		//function to create linked list.
{
  struct node *frntNode, *tmp;
  int num, i;

  stnode = (struct node *) malloc (sizeof (struct node));
  if (stnode == NULL)
    {
      cout << "Memory can not be allocated";
    }
  else
    {

      cout << "Enter the data for node 1: ";
      cin >> num;
      cout << "\n";
      stnode->num = num;
      stnode->nextptr = NULL;	//Links the address field to NULL
      tmp = stnode;

      for (i = 2; i <= n; i++)
	{
	  frntNode = (struct node *) malloc (sizeof (struct node));


	  if (frntNode == NULL)	//If frntnode is null no memory cannot be allotted
	    {
	      cout << "Memory can not be allocated";
	      break;
	    }
	  else
	    {
	      cout << "Enter the data for node " << i << ": ";	// Entering data in nodes. 
	      cin >> num;
	      cout << "\n";
	      frntNode->num = num;
	      frntNode->nextptr = NULL;
	      tmp->nextptr = frntNode;
	      tmp = tmp->nextptr;
	    }
	}
    }
}


void reverse (node * stnode)		//function to reverse linked list
{
  if (stnode == NULL)
    return;
  reverse (stnode->nextptr);

  cout << stnode->num << "\t";
}

Output

Enter the number of nodes: 5
Enter the data for node 1: 1
Enter the data for node 2: 2
Enter the data for node 3: 3
Enter the data for node 4: 4
Enter the data for node 5: 5
After reversing
5 4 3 2 1

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