Write a C function to reverse a given Doubly Linked List
Doubly Linked List:
Doubly Linked List (DLL) is a list of elements and it varies from Linked List. It allows navigation, either forward or backward when compared to Single Linked List. It has two pointers: previous pointer and next pointer. Every element points to next of the list and previous element in list.
Terms used in doubly linked list:
- Linked list
See below diagrams for example.
(a) Original Doubly Linked List
(b) Reversed Doubly Linked List
Here is a simple method for reversing a Doubly Linked List. All we need to do is swap prev and next pointers for all nodes, change prev of the head (or start) and change the head pointer in the end.
Original Linked list 10 8 4 2 Reversed Linked list 2 4 8 10
Time Complexity: O(n)
We can also swap data instead of pointers to reverse the Doubly Linked List. Method used for reversing array can be used to swap data. Swapping data can be costly compared to pointers if size of data item(s) is more.