# Reverse a Linked List C++ Code (Iterative and Recursive)

Many of you must be familiar with the application of a linked list in the real world and its importance. We use linked lists to maintain a directory of names, dynamic allocation of memory, and create an implementation of essentials data structures like stacks and queues, and what not?

Knowing all this in this tutorial we are going to discuss the basic understanding of a linked list, and implement and analyze how to reverse a linked list in C++. Let’s get Kraken!

The general definition is that a linked list is a sequence of data structures, the nodes are connected with pointers, and the last node points to NULL.

A linked list is a sequence of links that contain elements connected using pointers. Each link which is a pointer contains a connection to another node. The linked list is the second most utilized data structure after arrays.

There are three components of a linked list:

Node − Each node stores data which is called an element.

Next − Every node of a linked list contains a link to the next link that is Next. ## How to Reverse a Linked List?

Reversing the list implies reversing all the elements and we can do it by reversing all the links and make the next pointer point to the previous node.

### Problem Description

In this problem statement we are provided with the pointer or reference to the head of a singly linked list, invert the list, and return the pointer or reference to the head of the new reversed linked list.

For example, consider the following linked list: After reversing the complete linked list we return the pointer to the new linked list as demonstrated in the figure: There can be two approaches to solve this problem, the following are the hints, try solving the problem by yourself and then head over to the next section to find the solution and C++ code.

### Hints

1. Think of an iterative approach to find the reversed list in a single pass.
2. Or, think of a recursive approach to find the reversed list in a single pass.

Now that you have tried solving the problem yourself given the two types of approaches to solving this problem, let’s discuss both the approaches one by one:

## Iterative Solution for Reversing a Linked List

If the linked list has only one or no element, then we return the current list as it is. And if there are two or more elements, then we can implement an iterative solution using three-pointers

We will create a function to reverse the linked list taking reference to the head node and as the only argument and return the head of the new linked list:

Step 1: Define three nodes one with the reference to the head node and name it current, and name the other two nodes temp and prev pointers as NULL.

Step 2: Using a while loop we will traverse the linked list once until the next pointer does not become NULL.

Step 3: While iterating, we perform the following operations:

temp = current->next;

current->next = prev;

prev = current;

current = temp; We assign the temp node to the next of the current node and then reverse the link by assign the current->next to the previous node. And then increment the previous node to the current node and then the current node to the temp node.

And then we finally return the head node.

### Iterative Implementation

The iterative implementation of reversing a linked list in c++ follows:-

```#include<bits/stdc++.h>

using namespace std;

struct node {
int data;
struct node *next;
};

// To create a demo we have to construct a linked list and this
// function is to push the elements to the list.
void push(struct node **head_ref, int data) {
struct node *node;
node = (struct node*)malloc(sizeof(struct node));
node->data = data;
}

// Function to reverse the list
struct node *temp = NULL;
struct node *prev = NULL;
while(current != NULL) {
temp = current->next;
current->next = prev;
prev = current;
current = temp;
}
}

// To check our program
}
}

// Driver function
int main() {
cout << "Linked List Before Reversing" << endl;
cout << endl;
cout << "Linked List After Reversing"<<endl;
return 0;
}
```

10 4 0 8 1 0

0 1 8 0 4 10

### Time complexity:

O(N) because we iterate through each element at least once.

### Space complexity:

O(1) because no extra space was used here.

## Recursive solution for Reversing a Linked List

The most important thing to remember in this approach is that the recursive approach uses a stack. The compiler allocates stack memory after each recursive call, and this solution can run out of memory in case of very huge linked lists (think billions of elements).  We recursively iterate to each node in the list until we reach the last node and return the new head. We have to note that the last node in this approach will become the new head of the list. On the return path, each node is going to append itself to the end of the partially reversed linked list.

### Recursive Implementation

The recursive implementation of reversing a linked list in c++ follows:-

```#include
using namespace std;

struct Node {
int data;
struct Node* next;
Node(int data)
{
this->data = data;
next = NULL;
}
};

{
}

{
// Recursive call

return rest;
}

void print()
{
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
}

void push(int data)
{
Node* temp = new Node(data);
}
};

int main()
{
ll.push(320);
ll.push(34);
ll.push(315);
ll.push(385);

cout << "Linked List Before Reversing\n";
ll.print();

cout << "\nLinked List After Reversing \n";
ll.print();
return 0;
}
```

385 315 34 320

320 34 315 385

### Time Complexity:

O(N) because we iterate through each element at least once.

### Space complexity:

O(N) because we create a recursive stack each time we call the reverse function recursively.

## Conclusion

In this tutorial, we learned how to reverse a link using two approaches, and discussed the code in C++. This question is frequently asked in technical interviews by huge companies like Amazon and Google.

### FavTutor - 24x7 Live Coding Help from Expert Tutors! 