# How to reverse a doubly linked list?

It is easier to reverse a double-linked list than it is to reverse a single linked list. This operation reverses the nodes in the list of double links, making the first node the last node and the last node the first node.

Reverting a doubly linked list is an important operation. Here we swap the previous and next pointers for all nodes, as well as the head and tail pointers.

#### For example:

Here, the left and right pointers are swapped and moved towards each other until they meet or intersect. Then the first and last nodes are swapped.

#### Here Code:

```#include <iostream>
using namespace std;
struct Node {
int data;
struct Node *prev, *next;
};
Node* newNode(int val){
Node* temp = new Node;
temp->data = val;
temp->prev = temp->next = nullptr;
return temp;
}
cout << head->data << " <==> ";
}
}
Node* temp = newNode(node_data);
}
while (right->next != nullptr)
right = right->next;
while (left != right && left->prev != right) {
swap(left->data, right->data);
left = left->next;
right = right->prev;}
}
int main(){
cout << "Original doubly linked list: " << endl;
cout << "Reverse doubly linked list: " << endl;
return 0;
}```

### Complexity Analysis:

• Time Complexity: O(N), Where N is the number of nodes in the double linked list.
• Auxiliary Space: O(1)

### References:

Help Me Please: If you found any error in the content or example please let us know by comment also write your suggestion or if any doubt in the concept please feel free to write us in the comment section.

## Practice your Code online here

(Visited 82 times, 1 visits today)

Article Rating
Subscribe
Notify of
Inline Feedbacks

[…] How to reverse a doubly linked list? […]

[…] How to reverse a doubly linked list? […]