Write a program to reverse the given Doubly Linked List.
See below diagrams for example.
(a) Original Doubly Linked List

(b) Reversed Doubly Linked List
Approach: In the previous post, doubly linked list is being reversed by swapping prev and next pointers for all nodes, changing prev of the head (or start) and then changing the head pointer in the end. In this post, we create a push function that adds the given node at the beginning of the given list. We traverse the original list and one by one pass the current node pointer to the push function. This process will reverse the list. Finally return the new head of this reversed list.
// C++ implementation to reverse // a doubly linked list #include <bits/stdc++.h> using namespace std; // a node of the doubly linked list struct Node { int data; Node *next, *prev; }; // function to get a new node Node* getNode(int data) { // allocate space Node* new_node = (Node*)malloc(sizeof(Node)); // put in the data new_node->data = data; new_node->next = new_node->prev = NULL; return new_node; } // function to insert a node at the beginging // of the Doubly Linked List void push(Node** head_ref, Node* new_node) { // since we are adding at the begining, // prev is always NULL new_node->prev = NULL; // link the old list off the new node new_node->next = (*head_ref); // change prev of head node to new node if ((*head_ref) != NULL) (*head_ref)->prev = new_node; // move the head to point to the new node (*head_ref) = new_node; } // function to reverse a doubly linked list void reverseList(Node** head_ref) { // if list is empty or it contains // a single node only if (!(*head_ref) || !((*head_ref)->next)) return; Node* new_head = NULL; Node *curr = *head_ref, *next; while (curr != NULL) { // get pointer to next node next = curr->next; // push 'curr' node at the beginning of the // list with starting with 'new_head' push(&new;_head, curr); // update 'curr' curr = next; } // update 'head_ref' *head_ref = new_head; } // Function to print nodes in a // given doubly linked list void printList(Node* head) { while (head != NULL) { cout << head->data << " "; head = head->next; } } // Driver program to test above int main() { // Start with the empty list Node* head = NULL; // Create doubly linked: 10<->8<->4<->2 */ push(&head;, getNode(2)); push(&head;, getNode(4)); push(&head;, getNode(8)); push(&head;, getNode(10)); cout << "Original list: "; printList(head); // Reverse doubly linked list reverseList(&head;); cout << "\nReversed list: "; printList(head); return 0; } |
Output:
Original list: 10 8 4 2 Reversed list: 2 4 8 10
Time Complexity: O(n).
Recommended Posts:
- Reverse a Doubly Linked List
- Reverse a Doubly linked list using recursion
- Reverse a doubly circular linked list
- Reverse a Doubly Linked List | Set 4 (Swapping Data)
- Reverse a doubly linked list in groups of given size
- Print Doubly Linked list in Reverse Order
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- QuickSort on Doubly Linked List
- Doubly Linked List | Set 1 (Introduction and Insertion)
- Sort a k sorted doubly linked list
- Doubly Circular Linked List | Set 2 (Deletion)
- Delete all occurrences of a given key in a doubly linked list
- Sort the biotonic doubly linked list
- Find pairs with given sum in doubly linked list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



