🔄 Doubly Linked List - Reverse In-Place

Visualize how a doubly linked list is reversed by swapping prev/next pointers!

🎯 Problem: Reverse a Doubly Linked List

Hi! I'm Teju 👋 We're going to reverse a doubly linked list in-place (without using extra space).

Input: Number of nodes N, followed by N integer values.

Output: The same list with nodes in reverse order.

Constraints:

• 1 ≤ N ≤ 1000

• Node values: -1000 ≤ value ≤ 1000

Example: 1 ↔ 2 ↔ 3 ↔ 4 ↔ 5 → 5 ↔ 4 ↔ 3 ↔ 2 ↔ 1

⚙️ How to Reverse In-Place

We simply swap the prev and next pointers of every node and update head at the end!

Algorithm Steps:

  1. If list is empty or has one node → already reversed
  2. Traverse the list, for each node:
    • Swap node.prev and node.next
  3. Finally, set head = original tail (which is now the first node after reversal)
null
1
2
3
null

Example original list

🎮 Interactive Demo

Enter the number of nodes and values to build the list...

Empty List ↔ null

📋 Final Reversed List