
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Reverse a Linked List using C++
In this article, we need to reverse the links with the help of a singly linked list. Our task is to create a function that is capable of reversing the given singly linked list. For example
Input: Following Linked list : 1->2->3->4->NULL Output: After processing of our function: 4->3->2->1->NULL
Approach to find The Solution
There are different approaches to reverse a linked list. Generally, a simple approach comes to our mind to traverse the list and reverse it while going through it.
Simple Approach
We will go through the linked list in this approach and try to reverse it while going through it.
Example
#include<bits/stdc++.h> using namespace std; struct Node { int data; struct Node* next; Node(int data) { this->data = data; next = NULL; } }; struct LinkedList { Node* head; LinkedList() { head = NULL; } // Function to print linked list void reverse() { auto curr = head; // current pointer Node* prev = NULL; // previous pointer while(curr) { auto temp = curr -> next; curr -> next = prev; prev = curr; head = prev; curr = temp; } } void print() { struct Node* temp = head; while (temp != NULL) { cout << temp->data << " "; temp = temp->next; } } void push(int data) { Node* temp = new Node(data); temp->next = head; head = temp; } }; int main() { LinkedList list; list.push(20); list.push(4); list.push(15); list.push(85); list.print(); list.reverse(); cout << "\n"; list.print(); }
Output
85 15 4 20 20 4 15 85
In this approach, we are simply traversing through the list and reversing as we go through it. It is a good approach as time complexity is O(N), where N is the size of our list.
Now we try to do one experiment where we try to use the stack for reversing the list.
Approach with Stack
We will use a stack to store all the nodes in this program and reverse them by going through the stack.
Example
#include<bits/stdc++.h> using namespace std; struct Node { int data; struct Node* next; Node(int data) { this->data = data; next = NULL; } }; struct LinkedList { Node* head; LinkedList() { head = NULL; } // Function to print linked list void reverse() { auto curr = head; // current pointer Node* prev = NULL; // previous pointer stack<Node *> s; while(curr) { s.push(curr); curr = curr -> next; } prev = s.top(); head = prev; s.pop(); while(!s.empty()) { auto temp = s.top(); s.pop(); prev -> next = temp; prev = temp; } prev -> next = NULL; } void print() { struct Node* temp = head; while (temp != NULL) { cout << temp->data << " "; temp = temp->next; } } void push(int data) { Node* temp = new Node(data); temp->next = head; head = temp; } }; int main() { LinkedList list; list.push(20); list.push(4); list.push(15); list.push(85); list.print(); list.reverse(); cout << "\n"; list.print(); }
Output
85 15 4 20 20 4 15 85
Explanation of the Above Code
In this approach, we store the list nodes in a stack while going through it and then using the stack to pop them and reverse the list; this approach also has a time complexity of O(N), where N is our list size. As earlier, we were using stack, so we can also use a recursive approach as that also uses the stack, so now we will make a recursive approach.
Recursive Approach
In this approach, we will do the same process as earlier but with recursive calls.
Example
#include<bits/stdc++.h> using namespace std; struct Node { int data; struct Node* next; Node(int data) { this->data = data; next = NULL; } }; struct LinkedList { Node* head; LinkedList() { head = NULL; } // Function to print linked list void rreverse(Node *curr, Node *prev) { if(curr == NULL) { // prev -> next = curr; head = prev; return; } rreverse(curr -> next, curr); curr -> next = prev; prev -> next = NULL; } void reverse() { auto curr = head; // current pointer Node* prev = NULL; // previous pointer rreverse(curr -> next, curr); } void print() { struct Node* temp = head; while (temp != NULL) { cout << temp->data << " "; temp = temp->next; } } void push(int data) { Node* temp = new Node(data); temp->next = head; head = temp; } }; int main() { LinkedList list; list.push(20); list.push(4); list.push(15); list.push(85); list.print(); list.reverse(); cout << "\n"; list.print(); }
Output
85 15 4 20 20 4 15 85
In this approach, we are doing the same as earlier, but with recursive calls, so this approach also has a time complexity of O(N), where N is the size of our list.
Conclusion
In this article, we solve the problem of Reversing a singly linked list. We also learned the C++ program for this problem and the complete approach ( Normal and two other approaches) by which we solved this problem. We can write the same program in other languages such as C, java, python, and other languages. We hope you find this article helpful.
- Related Articles
- Print Reverse a linked list using Stack
- Reverse a Doubly Linked List using C++
- Reverse Linked List in Python
- C Program for Reverse a linked list
- Reverse a Linked List in groups of a Given Size using C++
- How to Reverse a linked list in android?
- Program to reverse a linked list in Python
- Golang Program to reverse a given linked list.
- Reverse a Doubly-Linked List in Groups of a Given Size using C++
- Reverse Linked List II in C++\n
- Python Program to Display the Nodes of a Linked List in Reverse using Recursion
- C program to display numbers in reverse order using single linked list
- Print Immutable Linked List in Reverse in C++
- Python Program to Display the Nodes of a Linked List in Reverse without using Recursion
- Program to reverse inner nodes of a linked list in python
