C Program for Printing reverse of a Linked List without actually reversing
Reverse printing a Linked List without actually reversing the Linked List
Here we will learn how to code a C program for Reverse Printing a Linked List, without actually reversing it. This is a pretty simple problem in which we will make a linked list, and instead of printing it in the usual manner we will traverse to the tail of the Linked List, and will print it in the reverse order. Let’s see how to code a program for this problem.
How do you reverse a Linked List ?
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
Algorithm for Reverse Printing a Linked List
- CREATE A FUNCTION DISPLAY
- if, HEAD = NULL
- RETURN 0
- ELSE
- RECURSIVELY CALL DISPLAY(HEAD->NEXT)
- PRINT(HEAD->DATA)
C Program for Reverse printing a Linked List
#include<stdio.h> #include<stdlib.h> struct node //code for making a node { int data; struct node *next; }; void display (struct node *head) //method for reverse display nodes { if (head == NULL) return; // print the list after head node display (head->next); // After everything else is printed, print head printf ("%d ", head->data); } int main () { struct node *prev, *head, *p; int n, i; printf ("Enter size of Linked List: "); scanf ("%d", &n); head = NULL; for (i = 0; i < n; i++) { p = malloc (sizeof (struct node)); printf ("Enter the data: "); scanf ("%d", &p->data); p->next = NULL; if (head == NULL) head = p; else prev->next = p; prev = p; } display (head); return 0; }
Output
Enter size of Linked List: 5
Enter the data: 1
Enter the data: 2
Enter the data: 3
Enter the data: 4
Enter the data: 5
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
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
- Deletion 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
Singly Linked List
- Introduction to Linked List in Data Structure
Click Here - Linked List in –
- Singly Linked List in –
- Insertion in singly Linked List –
- Insertion at beginning in singly Linked List –
- Insertion at nth position in singly Linked List –
- Insertion at end in singly Linked List –
- Deletion in singly Linked List –
- Deletion from beginning in singly linked list :
- Deletion from nth position in singly linked list :
- Deletion from end in singly linked list :
- 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 –
- Print reverse of a linked list without actually reversing –
- Print reverse of a linked list without actually reversing –
- Insertion in the middle Singly Linked List –
- Insertion in a Sorted Linked List –
- Delete alternate nodes of a Linked List –
- Find middle of the linked list –
- Reverse a linked list in groups of given size –
- Find kth node from end of the linked list –
- Append the last n nodes of a linked list to the beginning of the list –
- Check whether linked list is palindrome or not –
- Fold a Linked List –
- Insert at given Position –
- Deletion at given Position –
Login/Signup to comment