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)

Reverse a Doubly Linked List

beginner

Reverses a doubly linked list by swapping prev and next pointers for every node; O(n) time, O(1) space.

Initializing
curr
null
temp
null
NULL1481570067006259774815481533932597759774NULL3932currhead
Initial DLL: 1 ↔ 2 ↔ 3 ↔ 4. Each node stores a value plus prev (left) and next (right) address pointers. We will reverse all pointers by swapping next ↔ prev for each node.