# 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:
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;
}
};
// 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;
curr = temp;
}
}
void print() {
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
}
void push(int data) {
Node* temp = new Node(data);
}
};
int main() {
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;
}
};
// 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();
s.pop();
while(!s.empty()) {
auto temp = s.top();
s.pop();
prev -> next = temp;
prev = temp;
}
prev -> next = NULL;
}
void print() {
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
}
void push(int data) {
Node* temp = new Node(data);
}
};
int main() {
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;
}
};
// Function to print linked list
void rreverse(Node *curr, Node *prev) {
if(curr == NULL) {
// prev -> next = curr;
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() {
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
}
void push(int data) {
Node* temp = new Node(data);
}
};
int main() {
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.