Reverse a Doubly Linked List (original) (raw)
Last Updated : 27 Feb, 2026
Given the **head of a Doubly Linked List, reverse the list in-place so that the first node becomes the last, the second node becomes the second last, and so on. Return the new head of the reversed list.
**Examples:
**Input:
**Output: 3 <-> 2 <-> 1 -> NULL
**Input:
**Output: 1 ->NULL
**Explanation: Reverse of single node is same.
Table of Content
- [Naive Approach] Using Recursion - O(n) Time and O(n) Space
- [Expected Approach] Using Two Pointers - O(n) Time and O(1) Space
[Naive Approach] Using Recursion - O(n) Time and O(n) Space
The idea is to reverse the doubly linked list by swapping the next and prev pointers of each node. Once the pointers of the current node are swapped, we make a recursive call on the new prev pointer (which originally was the next node) to process the rest of the list.
C++ `
#include using namespace std;
class Node { public: int data; Node *next, *prev;
Node(int val) {
data = val;
next = nullptr;
prev = nullptr;
}};
Node *reverse(Node *curr) {
// Base case: if the list is empty or we
// reach the end of the list
if (curr == nullptr)
return nullptr;
swap(curr->prev, curr->next);
// If the previous node (after swap) is null,
// this is the new head
if (curr->prev == nullptr)
return curr;
return reverse(curr->prev);}
void printList(Node *node) { while (node != nullptr) { cout << node->data; if(node->next != nullptr){ cout << " <-> "; } node = node->next; } }
int main() {
Node *head = new Node(1);
head->next = new Node(2);
head->next->prev = head;
head->next->next = new Node(3);
head->next->next->prev = head->next;
head = reverse(head);
printList(head);
return 0;}
C
#include <stdio.h>
struct Node { int data; struct Node *next; struct Node *prev; };
struct Node *reverse(struct Node *curr) {
// Base case: If the list is empty or we
// reach the end of the list
if (curr == NULL) return NULL;
// Swap the next and prev pointers
struct Node *temp = curr->prev;
curr->prev = curr->next;
curr->next = temp;
// If the previous node (after swap) is NULL
// his is the new head
if (curr->prev == NULL) return curr;
return reverse(curr->prev);}
void printList(struct Node *node) { while (node != NULL) { printf("%d ", node->data); if(node->next != NULL){ printf("<-> "); } node = node->next; } printf("\n"); }
struct Node *createNode(int new_data) { struct Node *new_node = (struct Node *)malloc(sizeof(struct Node)); new_node->data = new_data; new_node->next = NULL; new_node->prev = NULL; return new_node; }
int main() {
struct Node *head = createNode(1);
head->next = createNode(2);
head->next->prev = head;
head->next->next = createNode(3);
head->next->next->prev = head->next;
head = reverse(head);
printList(head);
return 0;}
Java
class GfG { static Node head;
static class Node {
int data;
Node next, prev;
Node(int d) {
data = d;
next = prev = null;
}
}
static Node reverse(Node curr) {
// Base case: If list is empty or we
// reach the end of the list
if (curr == null)
return null;
// Swap the next and prev pointers
Node temp = curr.prev;
curr.prev = curr.next;
curr.next = temp;
// If prev is null after swap, this is the new head
if (curr.prev == null) {
return curr;
}
return reverse(curr.prev);
}
static void printList(Node node) {
while (node != null) {
System.out.print(node.data);
if(node.next != null){
System.out.print(" <-> ");
}
node = node.next;
}
System.out.println();
}
public static void main(String[] args) {
head = new Node(1);
head.next = new Node(2);
head.next.prev = head;
head.next.next = new Node(3);
head.next.next.prev = head.next;
head = reverse(head);
printList(head);
}}
Python
class Node: def init(self, val): self.data = val self.next = None self.prev = None
def reverse(curr):
# Base case: if the list is
# empty or we reach the end of the list
if curr is None:
return None
# Swap the next and prev pointers
temp = curr.prev
curr.prev = curr.next
curr.next = temp
if curr.prev is None:
return curr
return reverse(curr.prev)def printList(node): while node is not None: print(node.data, end="") if node.next is not None: print(" <-> ", end="") node = node.next print()
if name == "main":
head = Node(1)
head.next = Node(2)
head.next.prev = head
head.next.next = Node(3)
head.next.next.prev = head.next
head = reverse(head)
printList(head)C#
using System;
public class Node { public int Data; public Node next; public Node Prev;
public Node(int val) {
Data = val;
next = null;
Prev = null;
}}
class GfG {
static Node reverse(Node curr) {
// Base case: if the list is empty or we reach the
// end of the list
if (curr == null)
return null;
// Swap the next and prev pointers
Node temp = curr.Prev;
curr.Prev = curr.next;
curr.next = temp;
// If the previous node (after swap) is null,
// this is the new head
if (curr.Prev == null)
return curr;
return reverse(curr.Prev);
}
static void printList(Node node) {
while (node != null) {
Console.Write(node.Data);
if(node.next != null){
Console.Write(" <-> ");
}
node = node.next;
}
Console.WriteLine();
}
static void Main() {
Node head = new Node(1);
head.next = new Node(2);
head.next.Prev = head;
head.next.next = new Node(3);
head.next.next.Prev = head.next;
head = reverse(head);
printList(head);
}}
JavaScript
class Node { constructor(val) { this.data = val; this.next = null; this.prev = null; } }
function reverse(curr) {
// Base case: if the list is empty
// or we reach the end of the list
if (curr === null) {
return null;
}
// Swap the next and prev pointers
const temp = curr.prev;
curr.prev = curr.next;
curr.next = temp;
// If the previous node (after swap) is null,
// this is the new head
if (curr.prev === null) {
return curr;
}
return reverse(curr.prev);}
function printList(node) { let output = ''; while (node !== null) { output += node.data; if(node.next !== null) output += " <-> "; node = node.next; } console.log(output.trim()); }
// Driver Code
const head = new Node(1); head.next = new Node(2); head.next.prev = head; head.next.next = new Node(3); head.next.next.prev = head.next;
const reversedHead = reverse(head); printList(reversedHead);
`
[Expected Approach] Using Two Pointers - O(n) Time and O(1) Space
The idea is to reverse doubly linked list using two pointers for traversing through the list and swapping the next and previous pointers of every two consecutive nodes.
**Step-by-step algorithm:
- Initialize prevNode as NULL and set currNode to the head of the list.
- Traverse the linked list until currNode becomes NULL.
- For each node, swap currNode->next and currNode->prev.
- Move currNode to the next node using currNode = currNode->prev.
- After traversal, prevNode points to the second node of the reversed list.
- Update the head as head = prevNode->prev.
- Return the updated head. C++ `
#include using namespace std;
class Node { public: int data; Node *next; Node *prev; Node(int new_data) { data = new_data; next = NULL; prev = NULL; } };
Node *reverse(Node *head) {
// If the list is empty or has only one node,
// return the head as is
if (head == nullptr || head->next == nullptr)
return head;
Node *prevNode = NULL;
Node *currNode = head;
// Traverse the list and reverse the links
while (currNode != nullptr) {
// Swap the next and prev pointers
prevNode = currNode->prev;
currNode->prev = currNode->next;
currNode->next = prevNode;
// Move to the next node in the original list
// (which is now previous due to reversal)
currNode = currNode->prev;
}
return prevNode->prev;}
void printList(Node *node) { while (node != nullptr) { cout << node->data; if(node->next != nullptr){ cout << " <-> "; } node = node->next; } cout << endl; }
int main() {
Node *head = new Node(1);
head->next = new Node(2);
head->next->prev = head;
head->next->next = new Node(3);
head->next->next->prev = head->next;
head = reverse(head);
printList(head);
return 0;}
C
#include <stdio.h> struct Node { int data; struct Node *next; struct Node *prev; };
struct Node *reverse(struct Node *head) { if (head == NULL || head->next == NULL) return head;
struct Node *prevNode = NULL;
struct Node *currNode = head;
// Traverse the list and reverse the links
while (currNode != NULL) {
// Swap the next and prev pointers
prevNode = currNode->prev;
currNode->prev = currNode->next;
currNode->next = prevNode;
// Move to the next node in the original list
// (which is now previous due to reversal)
currNode = currNode->prev;
}
if (prevNode != NULL)
head = prevNode->prev;
return head;}
void printList(struct Node *node) { while (node != NULL) { printf("%d", node->data); if(node->next != NULL){ printf(" <-> "); } node = node->next; } printf("\n"); }
struct Node *createNode(int new_data){ struct Node *new_node = (struct Node *)malloc(sizeof(struct Node)); new_node->data = new_data; new_node->next = NULL; new_node->prev = NULL; return new_node; }
int main() {
struct Node *head = createNode(1);
head->next = createNode(2);
head->next->prev = head;
head->next->next = createNode(3);
head->next->next->prev = head->next;
head = reverse(head);
printList(head);
return 0;}
Java
class Node { int data; Node next; Node prev;
Node(int data) {
this.data = data;
this.next = null;
this.prev = null;
}}
public class GfG {
static Node reverse(Node head) {
if (head == null || head.next == null) {
return head;
}
Node currNode = head;
Node prevNode = null;
// Traverse the list and reverse the links
while (currNode != null) {
// Swap the next and prev pointers
prevNode = currNode.prev;
currNode.prev = currNode.next;
currNode.next = prevNode;
// Move to the next node in the original list
// (which is now previous due to reversal)
currNode = currNode.prev;
}
head = prevNode.prev;
return head;
}
static void printList(Node node) {
while (node != null) {
System.out.print(node.data);
if(node.next != null){
System.out.print(" <-> ");
}
node = node.next;
}
System.out.println();
}
public static void main(String[] args) {
Node head = new Node(1);
head.next = new Node(2);
head.next.prev = head;
head.next.next = new Node(3);
head.next.next.prev = head.next;
head = reverse(head);
printList(head);
}}Python
class Node: def init(self, new_data): self.data = new_data self.next = None self.prev = None
def reverse(head):
# If the list is empty or has only one node,
# return the head as is
if head is None or head.next is None:
return head
prevNode = None
currNode = head
# Traverse the list and reverse the links
while currNode is not None:
# Swap the next and prev pointers
prevNode = currNode.prev
currNode.prev = currNode.next
currNode.next = prevNode
# Move to the next node in the original list
# (which is now previous due to reversal)
currNode = currNode.prev
return prevNode.prevdef printList(node): while node is not None: print(node.data, end="") if node.next is not None: print(" <-> ", end="") node = node.next print()
if name == "main":
head = Node(1)
head.next = Node(2)
head.next.prev = head
head.next.next = Node(3)
head.next.next.prev = head.next
head = reverse(head)
printList(head)C#
using System;
class Node { public int Data; public Node next; public Node prev;
public Node(int newData) {
Data = newData;
next = null;
prev = null;
}}
class GfG {
static Node reverse(Node head) {
// If the list is empty or has only one node,
// return the head as is
if (head == null || head.next == null)
return head;
Node prevNode = null;
Node currNode = head;
// Traverse the list and reverse the links
while (currNode != null) {
// Swap the next and prev pointers
prevNode = currNode.prev;
currNode.prev = currNode.next;
currNode.next = prevNode;
// Move to the next node in the original list
// (which is now previous due to reversal)
currNode = currNode.prev;
}
return prevNode.prev;
}
static void printList(Node node) {
while (node != null) {
Console.Write(node.Data);
if(node.next != null){
Console.Write(" <-> ");
}
node = node.next;
}
Console.WriteLine();
}
public static void Main() {
Node head = new Node(1);
head.next = new Node(2);
head.next.prev = head;
head.next.next = new Node(3);
head.next.next.prev = head.next;
head = reverse(head);
printList(head);
}}
JavaScript
class Node { constructor(new_data) { this.data = new_data; this.next = null; this.prev = null; } }
function reverse(head) {
// If the list is empty or has only one node,
// return the head as is
if (head === null || head.next === null)
return head;
let prevNode = null;
let currNode = head;
// Traverse the list and reverse the links
while (currNode !== null) {
// Swap the next and prev pointers
prevNode = currNode.prev;
currNode.prev = currNode.next;
currNode.next = prevNode;
// Move to the next node in the original list
// (which is now previous due to reversal)
currNode = currNode.prev;
}
return prevNode.prev;}
function printList(node) { let output = ""; while (node !== null) { output += node.data; if(node.next != null){ output += " <-> "; } node = node.next; } console.log(output); }
// Driver Code let head = new Node(1); head.next = new Node(2); head.next.prev = head; head.next.next = new Node(3); head.next.next.prev = head.next;
head = reverse(head); printList(head);
`


