Reverse a Doubly Linked List Visualization & Animation
Reverses a doubly linked list by swapping prev and next pointers for every node; O(n) time, O(1) space.
## What is it?
Reverse a doubly linked list so that the tail becomes the head. Because each node has both `prev` and `next` pointers, reversing swaps those pointers for every node.
## How it works
- Traverse the list node by node
- For each node, swap its `prev` and `next` pointers: `node.prev = node.next`, `node.next = temp_prev`
- After processing all nodes, the last processed node is the new head
**Alternative:** swap `head` and `tail` pointers and reverse the link direction between all nodes
## When to use
- Doubly linked list manipulation
- Reversing navigation history in a browser (doubly linked implementation)
## Key Points
- O(n) time, O(1) space
- Since `prev` and `next` are both updated per node, no temporary storage of the list is needed
- After reversal, the original head's `next` becomes null and its `prev` points to the new head
Category: algorithms
Difficulty: beginner
Time Complexity: O(n)
Space Complexity: O(1)
View Reverse a Doubly Linked List Visualization