Printing Linked List in Reverse Order without actually Reversing it

Printing Linked List in Reverse Order without actually Reversing it” is again one of the famous problems of the Linked List. Here, we are given a linked list and our task is to print the given linked list in reverse order without actually reversing it.

We can use recursion to solve the following problem.


Suppose, we need to print the given linked list in reverse order without actually reversing it, then:
LINKED LIST:       12 -> 13 -> 14 -> 15 -> 16
Output List:       16 -> 15 -> 14 -> 13 -> 12

C++ Program to print linked list in reverse order:

/* C++ Program to Reverse a Linked List without actually reversing it */
using namespace std;

/* strcuture of the Node of the Linked List */
typedef struct node
    int data;
    struct node *next;

/* Function to insert node at the end of the linked list */
void insert_end(Node **head, int ele)
    /* Creating a new node */
    Node *new_node;
    new_node = (struct node*)malloc(sizeof(struct node));
    new_node->data = ele;
    new_node->next = NULL;
    /* check whether the linked list is empty or not */
    /* If Linked List is empty, new node will become head of the linked list */
    if(*head == NULL)     
        *head = new_node;
    /* If Linked List is not empty, then traverse the complete linked list and last node
    of the linked list will points to the new node of the linked list */
    Node *temp = NULL;
    temp = (*head);
    while(temp->next != NULL)
    temp->next = new_node;

/* Function to print the Linked List */
void print_actual(Node *head)
    Node *temp = head;
    while(temp != NULL)
        cout << temp -> data << " ";
        temp = temp -> next;

/* Function to print the linked list in reverse order*/
void print_reverse(Node *head)
    cout<<head->data<<" ";

int main()
    Node *head = NULL;
    /* Insert Some Nodes in the Linked List */
    cout<<"Actual Linked List Elements:\n";
    cout<<"\nPrinting Reverse Order of the Linked List:\n";
Actual Linked List Elements:
12 13 14 15 18 
Printing Reverse Order of the Linked List:
18 15 14 13 12

Related Posts:

  1. Swap Adjacent Elements of the Linked List.
  2. Count All Occurrences of a Particular Node in a Linked List.
  3. Bubble Sort on Linked List.
  4. Detect a Loop in a Linked List.
  5. Find the Length of the Loop present in the Linked List.
  6. Detect and Remove Loop from a Linked List.
  7. Segregate Even and Odd Nodes of the Linked List.
  8. Delete Complete Linked List.
  9. Delete Nth Node of the Linked List.
  10. Delete without head pointer of the Linked List.
  11. Delete All Occurrences of particular node of the Linked List.
  12. Delete Alternate Nodes of the Linked List.
  13. Delete every ‘N’ Nodes After ‘M’ Nodes of the Linked List.
  14. Remove Duplicate Nodes from an Unsorted Linked List.
  15. Remove Duplicate Nodes from a Sorted Linked List.
  16. Find Union and Intersection of Two Linked List.
  17. Merge Two Sorted Linked List.
  18. Insert a New Node at the Sorted Linked List.
  19. Reverse a Linked List.
  20. Reverse a Linked List Using Stack.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *