Deletion in a Doubly Linked List
Deleting a node in a doubly linked list is very similar to deleting a node in a singly linked list. However, there is a little extra work required to maintain the links of both the previous and next nodes. In this article, we will learn about different ways to delete a node in a doubly linked list.
Example:
Input: DLL = 1 <-> 2 <->3 , Node = 1
Output: 2 <-> 3Input: DLL = 2 <-> 45 <-> 3 <-> 1, Node = 45
Output: 2 <-> 3<-> 1
Table of Content
Deletion at the Beginning in Doubly Linked List
The idea is to update the head of the doubly linked list to the node next to head node and if the new head is not NULL, then set the previous pointer of new head to NULL.

Deletion at the beginning of Doubly Linked List
To delete a node at the beginning in doubly linked list, we can use the following steps:
- Check if the list is empty, there is nothing to delete, return.
- Store the head pointer in a variable, say temp.
- Update the head of linked list to the node next to the current head, head = head->next.
- If the new head is not NULL, update the previous pointer of new head to NULL, head->prev = NULL.
Below is the implementation of the above approach:
// C++ Program to delete a node from the beginning
// of a doubly linked list using class for Node
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* prev;
Node* next;
Node(int d) {
data = d;
prev = next = nullptr;
}
};
// Deletes the first node (head) of the list
// and returns the new head
Node* delHead(Node* head) {
// If the list is empty, return nullptr
if (head == nullptr) {
return nullptr;
}
// Store the current head node to delete
Node* temp = head;
// Move head to the next node
head = head->next;
// Set the previous pointer of the
// new head to nullptr
if (head != nullptr) {
head->prev = nullptr;
}
// Free the memory of the old head node
delete temp;
// Return the new head of the list
return head;
}
void printList(Node* head) {
Node* curr = head;
while (curr != nullptr) {
cout << curr->data << " ";
curr = curr->next;
}
cout << endl;
}
int main() {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
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 = delHead(head);
printList(head);
return 0;
}
// C Program to delete a node from the
// beginning of Doubly Linked List
#include <stdio.h>
struct Node {
int data;
struct Node *prev;
struct Node *next;
};
// Function to delete the first node (head) of the
// list and return the second node as the new head
struct Node *delHead(struct Node *head) {
// If empty, return NULL
if (head == NULL)
return NULL;
// Store in temp for deletion later
struct Node *temp = head;
// Move head to the next node
head = head->next;
// Set prev of the new head
if (head != NULL)
head->prev = NULL;
// Free memory and return new head
free(temp);
return head;
}
void printList(struct Node *head) {
struct Node *curr = head;
while (curr != NULL) {
printf("%d ", curr->data);
curr = curr->next;
}
printf("\n");
}
struct Node *createNode(int data) {
struct Node *newNode =
(struct Node *)malloc(sizeof(struct Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
int main() {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
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 = delHead(head);
printList(head);
return 0;
}
// Java Program to delete a node from the
// beginning of Doubly Linked List
class Node {
int data;
Node next;
Node prev;
Node(int x) {
data = x;
next = null;
prev = null;
}
}
class GfG {
// Function to delete the first node (head) of the
// list and return the second node as the new head
static Node delHead(Node head) {
// If empty, return null
if (head == null) {
return null;
}
// Store in temp for deletion later
Node temp = head;
// Move head to the next node
head = head.next;
// Set prev of the new head
if (head != null) {
head.prev = null;
}
// Return new head
return head;
}
static void printList(Node head) {
Node curr = head;
while (curr != null) {
System.out.print(curr.data + " ");
curr = curr.next;
}
System.out.println();
}
public static void main(String[] args) {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
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 = delHead(head);
printList(head);
}
}
# Python Program to delete a node from the
# beginning of Doubly Linked List
class Node:
def __init__(self, data):
self.data = data
self.prev = None
self.next = None
# Function to delete the first node (head) of the list
# and return the second node as the new head
def del_head(head):
# If empty, return None
if head is None:
return None
# Store in temp for deletion later
temp = head
# Move head to the next node
head = head.next
# Set prev of the new head
if head is not None:
head.prev = None
# Return new head
return head
def print_list(head):
curr = head
while curr is not None:
print(curr.data, end=" ")
curr = curr.next
print()
if __name__ == "__main__":
# Create a hardcoded doubly linked list:
# 1 <-> 2 <-> 3
head = Node(1)
head.next = Node(2)
head.next.prev = head
head.next.next = Node(3)
head.next.next.prev = head.next
head = del_head(head)
print_list(head)
// C# Program to delete a node from the
// beginning of Doubly Linked List
using System;
class Node {
public int data;
public Node next;
public Node prev;
public Node(int x) {
data = x;
next = null;
prev = null;
}
}
class GfG {
// Deletes the first node (head) of the list
// and returns the second node as the new head
static Node DelHead(Node head) {
// If empty, return null
if (head == null)
return null;
// Move head to the next node
head = head.next;
// Set prev of the new head
if (head != null)
head.prev = null;
// Return new head
return head;
}
static void PrintList(Node head) {
Node curr = head;
while (curr != null) {
Console.Write(curr.data + " ");
curr = curr.next;
}
Console.WriteLine();
}
static void Main() {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
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 = DelHead(head);
PrintList(head);
}
}
// JavaScript Program to delete a node from the
// beginning of Doubly Linked List
class Node {
constructor(data) {
this.data = data;
this.prev = null;
this.next = null;
}
}
// Deletes the first node (head) of the list and
// returns the second node as the new head
function delHead(head) {
// If empty, return null
if (head === null) {
return null;
}
// Store in temp for deletion later
let temp = head;
// Move head to the next node
head = head.next;
// Set prev of the new head
if (head !== null) {
head.prev = null;
}
// Return new head
return head;
}
function printList(head) {
let curr = head;
let output = '';
while (curr !== null) {
output += curr.data + ' ';
curr = curr.next;
}
console.log(output.trim());
}
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
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 = delHead(head);
printList(head);
Output
2 3
Time Complexity: O(1), Since traversal of the linked list is not required.
Auxiliary Space: O(1)
Deletion after a given node in Doubly Linked List

Deletion after a given node in Doubly Linked List
To delete a node after a specific node in a doubly linked list, we can use the following steps:
- Initialize a variable , say curr points to the node with the key value in the linked list.
- if found , check if curr->next is not NULL.
- If it’s NULL then there is no node to delete , return.
- else , set a pointer nodeDelete to curr->next, which is the node to be deleted.
- Update curr->next to point to nodeDelete->next.
- If nodeDelete->next is not NULL, update the previous pointer of nodeDelete->next to curr.
- Delete nodeDelete to free memory.
// C++ Program to delete a node after
// a given node of doubly linked list
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node *next;
Node *prev;
Node(int new_data) {
data = new_data;
next = nullptr;
prev = nullptr;
}
};
// Function to delete a node after a given
// node in doubly linked list
Node *deleteAfter(Node *head, int key) {
Node *curr = head;
// Iterate over Linked List to find the key
while (curr != nullptr) {
if (curr->data == key)
break;
curr = curr->next;
}
// If curr is NULL or curr->next is NULL,
// there is no node to delete
if (curr == nullptr || curr->next == nullptr)
return head;
// Node to be deleted
Node *nodeDelete = curr->next;
// Update the next of the current node
// to the next of the node to be deleted
curr->next = nodeDelete->next;
// If the node to be deleted is not the last node,
// update the previous pointer of the next node
if (nodeDelete->next != nullptr) {
nodeDelete->next->prev = curr;
}
// Free the memory of the node to be deleted
delete nodeDelete;
return head;
}
void printList(Node *head) {
Node *curr = head;
while (curr != nullptr) {
cout << " " << curr->data;
curr = curr->next;
}
cout << endl;
}
int main() {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3 <-> 4
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->next->next->next = new Node(4);
head->next->next->next->prev = head->next->next;
head = deleteAfter(head, 2);
printList(head);
return 0;
}
// C Program to delete a node after a given
// node of doubly linked list
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
struct Node *prev;
};
// Function to delete a node after a given
// node in doubly linked list
struct Node *deleteAfter(struct Node *head, int key) {
struct Node *curr = head;
// Iterate over Linked List to find the key
while (curr != NULL) {
if (curr->data == key)
break;
curr = curr->next;
}
// If curr is NULL or curr->next is NULL,
// there is no node to delete
if (curr == NULL || curr->next == NULL)
return head;
// Node to be deleted
struct Node *nodeDelete = curr->next;
// Update the next of the current node to the
//next of the node to be deleted
curr->next = nodeDelete->next;
// If the node to be deleted is not the last node,
// update the previous pointer of the next node
if (nodeDelete->next != NULL) {
nodeDelete->next->prev = curr;
}
// Free the memory of the node to be deleted
free(nodeDelete);
return head;
}
void printList(struct Node *head) {
struct Node *curr = head;
while (curr != NULL) {
printf("%d ", curr->data);
curr = curr->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() {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3 <-> 4
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->next->next->next = createNode(4);
head->next->next->next->prev = head->next->next;
head = deleteAfter(head, 2);
printList(head);
return 0;
}
// Java Program to delete a node after a given node of
// doubly linked list
class Node {
int data;
Node next;
Node prev;
Node(int x) {
data = x;
next = null;
prev = null;
}
}
class GfG {
// Function to delete a node after a given node in
// doubly linked list
static Node deleteAfter(Node head, int key) {
Node curr = head;
// Iterate over Linked List to find the key
while (curr != null) {
if (curr.data == key)
break;
curr = curr.next;
}
// If curr is null or curr.next is null, there is no
// node to delete
if (curr == null || curr.next == null)
return head;
// Node to be deleted
Node nodeDelete = curr.next;
// Update the next of the current node to the next
// of the node to be deleted
curr.next = nodeDelete.next;
// If the node to be deleted is not the last node,
// update the previous pointer of the next node
if (nodeDelete.next != null) {
nodeDelete.next.prev = curr;
}
return head;
}
static void printList(Node head) {
Node curr = head;
while (curr != null) {
System.out.print(" " + curr.data);
curr = curr.next;
}
System.out.println();
}
public static void main(String[] args) {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3 <-> 4
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.next.next.next = new Node(4);
head.next.next.next.prev = head.next.next;
head = deleteAfter(head, 2);
printList(head);
}
}
# Python Program to delete a node after a given
# node of doubly linked list
class Node:
def __init__(self, new_data):
self.data = new_data
self.next = None
self.prev = None
def delete_after(head, key):
curr = head
# Iterate over Linked List to find the key
while curr is not None:
if curr.data == key:
break
curr = curr.next
# If curr is None or curr.next is None,
# there is no node to delete
if curr is None or curr.next is None:
return head
# Node to be deleted
node_delete = curr.next
# Update the next of the current node to
# the next of the node to be deleted
curr.next = node_delete.next
# If the node to be deleted is not the last node,
# update the previous pointer of the next node
if node_delete.next is not None:
node_delete.next.prev = curr
return head
def print_list(head):
curr = head
while curr is not None:
print(curr.data, end=" ")
curr = curr.next
print()
if __name__ == "__main__":
# Create a hardcoded doubly linked list:
# 1 <-> 2 <-> 3 <-> 4
head = Node(1)
head.next = Node(2)
head.next.prev = head
head.next.next = Node(3)
head.next.next.prev = head.next
head.next.next.next = Node(4)
head.next.next.next.prev = head.next.next
head = delete_after(head, 2)
print_list(head)
// C# Program to delete a node after a given node of doubly
// linked list
using System;
class Node {
public int data;
public Node next;
public Node prev;
public Node(int x) {
data = x;
next = null;
prev = null;
}
}
class GfG {
// Function to delete a node after a given node in
// doubly linked list
static Node DeleteAfter(Node head, int key) {
Node curr = head;
// Iterate over Linked List to find the key
while (curr != null) {
if (curr.data == key)
break;
curr = curr.next;
}
// If curr is null or curr.next is null, there is no
// node to delete
if (curr == null || curr.next == null)
return head;
// Node to be deleted
Node nodeToDelete = curr.next;
// Update the next of the current node to the next
// of the node to be deleted
curr.next = nodeToDelete.next;
// If the node to be deleted is not the last node,
// update the prev pointer of the next node
if (nodeToDelete.next != null) {
nodeToDelete.next.prev = curr;
}
return head;
}
static void PrintList(Node head) {
Node curr = head;
while (curr != null) {
Console.Write(" " + curr.data);
curr = curr.next;
}
Console.WriteLine();
}
static void Main(string[] args) {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3 <-> 4
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.next.next.next = new Node(4);
head.next.next.next.prev = head.next.next;
head = DeleteAfter(head, 2);
PrintList(head);
}
}
// JavaScript Program to delete a node after a given node of
// doubly linked list
class Node {
constructor(data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
// Function to delete a node after a given node in doubly
// linked list
function deleteAfter(head, key) {
let curr = head;
// Iterate over Linked List to find the key
while (curr !== null) {
if (curr.data === key)
break;
curr = curr.next;
}
// If curr is null or curr.next is null, there is no
// node to delete
if (curr === null || curr.next === null)
return head;
// Node to be deleted
let nodeToDelete = curr.next;
// Update the next of the current node to the next of
// the node to be deleted
curr.next = nodeToDelete.next;
// If the node to be deleted is not the last node,
// update the previous pointer of the next node
if (nodeToDelete.next !== null) {
nodeToDelete.next.prev = curr;
}
return head;
}
function printList(head) {
let curr = head;
while (curr !== null) {
console.log(curr.data + " ");
curr = curr.next;
}
console.log();
}
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3 <-> 4
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.next.next.next = new Node(4);
head.next.next.next.prev = head.next.next;
head = deleteAfter(head, 2);
printList(head);
Output
1 2 4
Time Complexity: O(n), where n is the number of nodes in doubly linked list.
Auxiliary Space: O(1)
Deletion before a given node in Doubly Linked List

Deletion before a given node in Doubly Linked List
To delete a node before a specific node in a doubly linked list, we can use the following steps:
- Initialize a variable , say curr points to the node with the key value in the linked list.
- if found , check if curr->prev is not NULL.
- If it’s NULL, the node to be deleted is the head node, so there is no node to delete before it.
- else , set a pointer nodeDelete to curr->prev, which is the node to be deleted.
- Update curr->prev to point to nodeDelete ->prev.
- If nodeDelete ->prev is not NULL, update nodeDelete->prev->next point to curr.
- Delete nodeDelete to free memory.
// C++ Program to delete a node before a given node
// of doubly linked list
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node *next;
Node *prev;
Node(int x) {
data = x;
next = nullptr;
prev = nullptr;
}
};
// Function to delete a node before a given
// node in a doubly linked list
Node *deleteBefore(Node *head, int key) {
Node *curr = head;
// Find the node with the given key
while (curr != nullptr) {
if (curr->data == key)
break;
curr = curr->next;
}
// If curr is nullptr or curr->prev is nullptr,
// there is no node to delete
if (curr == nullptr || curr->prev == nullptr)
return head;
// Node to be deleted
Node *nodeDelete = curr->prev;
// Update the prev of the current node
// to the prev of the node to be deleted
curr->prev = nodeDelete->prev;
// If nodeDelete's prev is not nullptr,
// update its next pointer to the current node
if (nodeDelete->prev != nullptr) {
nodeDelete->prev->next = curr;
}
else {
// If nodeDelete is the head node
head = curr;
}
// Free the memory of the node to be deleted
delete nodeDelete;
return head;
}
void printList(Node *head) {
Node *curr = head;
while (curr != nullptr)
{
cout << curr->data << " ";
curr = curr->next;
}
cout << endl;
}
int main() {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3 <-> 4
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->next->next->next = new Node(4);
head->next->next->next->prev = head->next->next;
head = deleteBefore(head, 3);
printList(head);
return 0;
}
// C Program to delete a node before a given node
// of doubly linked list
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
struct Node *prev;
};
// Function to delete a node before a given
// node in a doubly linked list
struct Node *deleteBefore(struct Node *head, int key) {
struct Node *curr = head;
// Find the node with the given key
while (curr != NULL) {
if (curr->data == key)
break;
curr = curr->next;
}
// If curr is NULL or curr->prev is NULL,
// there is no node to delete
if (curr == NULL || curr->prev == NULL)
return head;
// Node to be deleted
struct Node *nodeToDelete = curr->prev;
// Update the prev of the current node to
// the prev of the node to be deleted
curr->prev = nodeToDelete->prev;
// If nodeToDelete's prev is not NULL, update
// its next pointer to the current node
if (nodeToDelete->prev != NULL) {
nodeToDelete->prev->next = curr;
}
else {
// If nodeToDelete is the head node
head = curr;
}
// Free the memory of the node to be deleted
free(nodeToDelete);
return head;
}
void printList(struct Node *head) {
struct Node *curr = head;
while (curr != NULL) {
printf(" %d", curr->data);
curr = curr->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() {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3 <-> 4
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->next->next->next = createNode(4);
head->next->next->next->prev = head->next->next;
head = deleteBefore(head, 3);
printList(head);
return 0;
}
// Java Program to delete a node before a given node of
// doubly linked list
class Node {
int data;
Node next;
Node prev;
Node(int x) {
data = x;
next = null;
prev = null;
}
}
class GfG {
// Function to delete a node before a given node in a
// doubly linked list
static Node deleteBefore(Node head, int key) {
Node curr = head;
// Find the node with the given key
while (curr != null) {
if (curr.data == key)
break;
curr = curr.next;
}
// If curr is null or curr.prev is null, there is no
// node to delete
if (curr == null || curr.prev == null)
return head;
// Node to be deleted
Node nodeToDelete = curr.prev;
// Update the prev of the current node to the prev
// of the node to be deleted
curr.prev = nodeToDelete.prev;
// If nodeToDelete's prev is not null, update its
// next pointer to the current node
if (nodeToDelete.prev != null) {
nodeToDelete.prev.next = curr;
}
else {
// If nodeToDelete is the head node
head = curr;
}
return head;
}
static void printList(Node head) {
Node curr = head;
while (curr != null) {
System.out.print(curr.data + " ");
curr = curr.next;
}
System.out.println();
}
public static void main(String[] args) {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3 <-> 4
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.next.next.next = new Node(4);
head.next.next.next.prev = head.next.next;
head = deleteBefore(head, 3);
printList(head);
}
}
# Python Program to delete a node before a
# given node of doubly linked list
class Node:
def __init__(self, data):
self.data = data
self.next = None
self.prev = None
def delete_before(head, key):
curr = head
# Find the node with the given key
while curr is not None:
if curr.data == key:
break
curr = curr.next
# If curr is None or curr.prev is None,
# there is no node to delete
if curr is None or curr.prev is None:
return head
# Node to be deleted
nodeToDelete = curr.prev
# Update the prev of the current node to the prev
# of the node to be deleted
curr.prev = nodeToDelete.prev
# If nodeToDelete's prev is not None, update its
# next pointer to the current node
if nodeToDelete.prev is not None:
nodeToDelete.prev.next = curr
else:
# If nodeToDelete is the head node
head = curr
return head
def print_list(head):
curr = head
while curr:
print(curr.data, end=' ')
curr = curr.next
print()
if __name__ == "__main__":
# Create a hardcoded doubly linked list:
# 1 <-> 2 <-> 3 <-> 4
head = Node(1)
head.next = Node(2)
head.next.prev = head
head.next.next = Node(3)
head.next.next.prev = head.next
head.next.next.next = Node(4)
head.next.next.next.prev = head.next.next
head = delete_before(head, 3)
print_list(head)
// C# Program to delete a node before a given node of doubly
// linked list
using System;
class Node {
public int data;
public Node next;
public Node prev;
public Node(int x) {
data = x;
next = null;
prev = null;
}
}
class GfG {
// Function to delete a node before a given node in a
// doubly linked list
static Node DeleteBefore(Node head, int key) {
Node curr = head;
// Find the node with the given key
while (curr != null) {
if (curr.data == key)
break;
curr = curr.next;
}
// If curr is null or curr.prev is null, there is no
// node to delete
if (curr == null || curr.prev == null)
return head;
// Node to be deleted
Node nodeToDelete = curr.prev;
// Update the prev of the current node to the prev
// of the node to be deleted
curr.prev = nodeToDelete.prev;
// If nodeToDelete's prev is not null, update its
// next pointer to the current node
if (nodeToDelete.prev != null) {
nodeToDelete.prev.next = curr;
}
else {
// If nodeToDelete is the head node
head = curr;
}
return head;
}
static void PrintList(Node head) {
Node curr = head;
while (curr != null) {
Console.Write(curr.data + " ");
curr = curr.next;
}
Console.WriteLine();
}
static void Main() {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3 <-> 4
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.next.next.next = new Node(4);
head.next.next.next.prev = head.next.next;
head = DeleteBefore(head, 3);
PrintList(head);
}
}
// JavaScript Program to delete a node before a given node
// of doubly linked list
class Node {
constructor(data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
// Function to delete a node before a given node in a doubly
// linked list
function deleteBefore(head, key) {
let curr = head;
// Find the node with the given key
while (curr !== null) {
if (curr.data === key)
break;
curr = curr.next;
}
// If curr is null or curr.prev is null, there is no
// node to delete
if (curr === null || curr.prev === null)
return head;
// Node to be deleted
let nodeToDelete = curr.prev;
// Update the prev of the current node to the prev of
// the node to be deleted
curr.prev = nodeToDelete.prev;
// If nodeToDelete's prev is not null, update its next
// pointer to the current node
if (nodeToDelete.prev !== null) {
nodeToDelete.prev.next = curr;
}
else {
// If nodeToDelete is the head node
head = curr;
}
return head;
}
function printList(head) {
let curr = head;
while (curr !== null) {
console.log(curr.data + " ");
curr = curr.next;
}
console.log();
}
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3 <-> 4
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.next.next.next = new Node(4);
head.next.next.next.prev = head.next.next;
head = deleteBefore(head, 3);
printList(head);
Output
1 3 4
Time Complexity: O(n), where n is the number of nodes in doubly linked list
Auxiliary Space: O(1)
Deletion at a specific position in Doubly Linked List

Delete Node at position 2 in Doubly Linked List
To delete a node at a specific position in doubly linked list, we can use the following steps:
- Traverse to the node at the specified position, say curr.
- If the position is valid, adjust the pointers to skip the node to be deleted.
- If curr is not the head of the linked list, update the next pointer of the node before curr to point to the node after curr, curr->prev->next = curr->next.
- If curr is not the last node of the linked list, update the previous pointer of the node after curr to the node before curr, curr->next->prev = curr->prev.
- Free the memory allocated for the deleted node.
Below is the implementation of the above approach:
// C++ Program to delete a node at a specific position
// in Doubly Linked List
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node *prev;
Node *next;
Node(int d) {
data = d;
prev = nullptr;
next = nullptr;
}
};
// Function to delete a node at a specific position
// in the doubly linked list
Node *delPos(Node *head, int pos) {
// If the list is empty
if (head == nullptr) {
return head;
}
Node *curr = head;
// Traverse to the node at the given position
for (int i = 1; curr != nullptr && i < pos; ++i) {
curr = curr->next;
}
// If the position is out of range
if (curr == nullptr) {
return head;
}
// Update the previous node's next pointer
if (curr->prev != nullptr) {
curr->prev->next = curr->next;
}
// Update the next node's prev pointer
if (curr->next != nullptr) {
curr->next->prev = curr->prev;
}
// If the node to be deleted is the head node
if (head == curr) {
head = curr->next;
}
// Deallocate memory for the deleted node
delete curr;
return head;
}
void printList(Node *head) {
Node *curr = head;
while (curr != nullptr) {
cout << curr->data << " ";
curr = curr->next;
}
cout << endl;
}
int main() {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
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 = delPos(head, 2);
printList(head);
return 0;
}
// C Program to delete a node at a specific position
// in a doubly linked list
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
// Function to delete a node at a specific position
// in the doubly linked list
struct Node* delPos(struct Node* head, int pos) {
// If the list is empty
if (head == NULL) {
return head;
}
struct Node* curr = head;
// Traverse to the node at the given position
for (int i = 1; curr != NULL && i < pos; ++i) {
curr = curr->next;
}
// If the position is out of range
if (curr == NULL) {
return head;
}
// Update the previous node's next pointer
if (curr->prev != NULL) {
curr->prev->next = curr->next;
}
// Update the next node's prev pointer
if (curr->next != NULL) {
curr->next->prev = curr->prev;
}
// If the node to be deleted is the head node
if (head == curr) {
head = curr->next;
}
// Deallocate memory for the deleted node
free(curr);
return head;
}
void printList(struct Node* head) {
struct Node* curr = head;
while (curr != NULL) {
printf("%d ", curr->data);
curr = curr->next;
}
printf("\n");
}
struct Node* createNode(int data) {
struct Node* newNode =
(struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
int main() {
// Create a hardcoded doubly linked list: 1 <-> 2 <-> 3
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 = delPos(head, 2);
printList(head);
return 0;
}
// Java Program to delete node at a specific position
// in Doubly Linked List
class Node {
int data;
Node prev;
Node next;
Node(int d) {
data = d;
prev = null;
next = null;
}
}
class GfG {
// Function to delete a node at a
// specific position in the doubly linked list
static Node delPos(Node head, int pos) {
// If the list is empty
if (head == null) {
return head;
}
Node curr = head;
// Traverse to the node at the given position
for (int i = 1; curr != null && i < pos; ++i) {
curr = curr.next;
}
// If the position is out of range
if (curr == null) {
return head;
}
// Update the previous node's next pointer
if (curr.prev != null) {
curr.prev.next = curr.next;
}
// Update the next node's prev pointer
if (curr.next != null) {
curr.next.prev = curr.prev;
}
// If the node to be deleted is the head node
if (head == curr) {
head = curr.next;
}
// Return the updated head
return head;
}
static void printList(Node head) {
Node curr = head;
while (curr != null) {
System.out.print(curr.data + " ");
curr = curr.next;
}
System.out.println();
}
public static void main(String[] args) {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
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 = delPos(head, 2);
printList(head);
}
}
# Python Program to delete node at a specific position
# in Doubly Linked List
class Node:
def __init__(self, data):
self.data = data
self.prev = None
self.next = None
# Function to delete a node at a specific position
#in the doubly linked list
def del_pos(head, pos):
# If the list is empty
if head is None:
return head
curr = head
# Traverse to the node at the given position
for i in range(1, pos):
if curr is None:
return head
curr = curr.next
# If the position is out of range
if curr is None:
return head
# Update the previous node's next pointer
if curr.prev is not None:
curr.prev.next = curr.next
# Update the next node's prev pointer
if curr.next is not None:
curr.next.prev = curr.prev
# If the node to be deleted is the head node
if head == curr:
head = curr.next
# Return the updated head
return head
def print_list(head):
curr = head
while curr is not None:
print(curr.data, end=" ")
curr = curr.next
print()
if __name__ == "__main__":
# Create a hardcoded doubly linked list:
# 1 <-> 2 <-> 3
head = Node(1)
head.next = Node(2)
head.next.prev = head
head.next.next = Node(3)
head.next.next.prev = head.next
head = del_pos(head, 2)
print_list(head)
// C# Program to delete node at a specific position
// in Doubly Linked List
using System;
class Node {
public int Data;
public Node prev;
public Node next;
public Node(int data) {
Data = data;
prev = null;
next = null;
}
}
class GfG {
// Function to delete a node at a specific position
// in the doubly linked list
static Node DelPos(Node head, int pos) {
// If the list is empty
if (head == null)
return head;
Node curr = head;
// Traverse to the node at the given position
for (int i = 1; curr != null && i < pos; ++i) {
curr = curr.next;
}
// If the position is out of range
if (curr == null)
return head;
// Update the previous node's next pointer
if (curr.prev != null)
curr.prev.next = curr.next;
// Update the next node's prev pointer
if (curr.next != null)
curr.next.prev = curr.prev;
// If the node to be deleted is the head node
if (head == curr)
head = curr.next;
// Deallocate memory for the deleted node
// In C#, garbage collection will handle this
// automatically
return head;
}
static void PrintList(Node head) {
Node curr = head;
while (curr != null) {
Console.Write(curr.Data + " ");
curr = curr.next;
}
Console.WriteLine();
}
static void Main() {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
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 = DelPos(head, 2);
PrintList(head);
}
}
// Javascript Program to delete node at a specific position
// in Doubly Linked List
class Node {
constructor(data) {
this.data = data;
this.prev = null;
this.next = null;
}
}
// Function to delete a node at a specific position
// in the doubly linked list
function delPos(head, pos) {
// If the list is empty
if (head === null)
return head;
let curr = head;
// Traverse to the node at the given position
for (let i = 1; curr && i < pos; ++i) {
curr = curr.next;
}
// If the position is out of range
if (curr === null)
return head;
// Update the previous node's next pointer
if (curr.prev) {
curr.prev.next = curr.next;
}
// Update the next node's prev pointer
if (curr.next) {
curr.next.prev = curr.prev;
}
// If the node to be deleted is the head node
if (head === curr) {
head = curr.next;
}
// Deallocate memory for the deleted node
// In JavaScript, garbage collection handles
// this automatically
return head;
}
function printList(head) {
let curr = head;
let result = [];
while (curr !== null) {
result.push(curr.data);
curr = curr.next;
}
console.log(result.join(" "));
}
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
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 = delPos(head, 2);
printList(head);
Output
1 3
Time Complexity: O(n), where n is the number of nodes in the doubly linked list.
Auxiliary Space: O(1)
Deletion at the End in Doubly Linked List

Deletion at the End of Doubly Linked List
To delete a node at the end in doubly linked list, we can use the following steps:
- Check if the doubly linked list is empty. If it is empty, then there is nothing to delete.
- If the list is not empty, then move to the last node of the doubly linked list, say curr.
- Update the second-to-last node’s next pointer to NULL, curr->prev->next = NULL.
- Free the memory allocated for the node that was deleted.
Below is the implementation of the above approach:
// C++ Program to delete a node from the
// end of Doubly Linked List
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* prev;
Node* next;
Node(int d) {
data = d;
prev = next = nullptr;
}
};
// Function to delete the last node of
// the doubly linked list
Node* delLast(Node* head) {
if (head == nullptr) {
return nullptr;
}
if (head->next == nullptr) {
delete head;
return nullptr;
}
// Traverse to the last node
Node* curr = head;
while (curr->next != nullptr) {
curr = curr->next;
}
// Update the previous node's next pointer
if (curr->prev != nullptr) {
curr->prev->next = nullptr;
}
// Delete the last node
delete curr;
// Return the updated head
return head;
}
void printList(Node* head) {
Node* curr = head;
while (curr != nullptr) {
cout << curr->data << " ";
curr = curr->next;
}
cout << endl;
}
int main() {
// Create a hardcoded doubly linked list: 1 <-> 2 <-> 3
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 = delLast(head);
printList(head);
return 0;
}
// C Program to delete a node from the
// end of Doubly Linked List
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
// Function to delete the last node
// of the doubly linked list
struct Node* delLast(struct Node *head) {
if (head == NULL)
return NULL;
if (head->next == NULL) {
free(head);
return NULL;
}
// Traverse to the last node
struct Node *curr = head;
while (curr->next != NULL)
curr = curr->next;
// Update the previous node's next pointer
curr->prev->next = NULL;
// Delete the last node
free(curr);
// Return the updated head
return head;
}
void printList(struct Node *head) {
struct Node *curr = head;
while (curr != NULL) {
printf("%d ", curr->data);
curr = curr->next;
}
printf("\n");
}
struct Node* createNode(int data) {
struct Node *newNode =
(struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
int main() {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
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 = delLast(head);
printList(head);
return 0;
}
// Java Program to delete a node from the
// end of Doubly Linked List
class Node {
int data;
Node prev;
Node next;
Node(int d) {
data = d;
prev = null;
next = null;
}
}
public class GfG {
// Function to delete the last node
// of the doubly linked list
static Node delLast(Node head) {
if (head == null) {
return null;
}
if (head.next == null) {
return null;
}
// Traverse to the last node
Node curr = head;
while (curr.next != null) {
curr = curr.next;
}
// Update the previous node's next pointer
if (curr.prev != null) {
curr.prev.next = null;
}
// Return the updated head
return head;
}
static void printList(Node head) {
Node curr = head;
while (curr != null) {
System.out.print(curr.data + " ");
curr = curr.next;
}
System.out.println();
}
public static void main(String[] args) {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
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 = delLast(head);
printList(head);
}
}
# Python Program to delete a node from
# the end of Doubly Linked List
class Node:
def __init__(self, data):
self.data = data
self.prev = None
self.next = None
def del_last(head):
if head is None:
return None
if head.next is None:
return None
# Traverse to the last node
curr = head
while curr.next is not None:
curr = curr.next
# Update the previous node's next pointer
if curr.prev is not None:
curr.prev.next = None
# Return the updated head
return head
def print_list(head):
curr = head
while curr is not None:
print(curr.data, end=" ")
curr = curr.next
print()
if __name__ == "__main__":
# Create a hardcoded doubly linked list:
# 1 <-> 2 <-> 3
head = Node(1)
head.next = Node(2)
head.next.prev = head
head.next.next = Node(3)
head.next.next.prev = head.next
head = del_last(head)
print_list(head)
// C# Program to delete a node from the
// end of Doubly Linked List
using System;
class Node {
public int Data;
public Node prev;
public Node next;
public Node(int data) {
Data = data;
prev = null;
next = null;
}
}
class GfG {
// Function to delete the last node of the doubly linked
// list
static Node DelLast(Node head) {
if (head == null)
return null;
if (head.next == null) {
return null;
}
// Traverse to the last node
Node curr = head;
while (curr.next != null)
curr = curr.next;
// Update the previous node's next pointer
if (curr.prev != null)
curr.prev.next = null;
// Delete the last node
curr = null;
// Return the updated head
return head;
}
static void PrintList(Node head) {
Node curr = head;
while (curr != null) {
Console.Write(curr.Data + " ");
curr = curr.next;
}
Console.WriteLine();
}
static void Main() {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
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 = DelLast(head);
PrintList(head);
}
}
// JavaScript Program to delete a node from
// the end of Doubly Linked List
class Node {
constructor(data) {
this.data = data;
this.prev = null;
this.next = null;
}
}
// Function to delete the last node of the doubly linked
// list
function delLast(head) {
if (head === null)
return null;
if (head.next === null) {
// Only one node in the list
return null;
}
// Traverse to the last node
let curr = head;
while (curr.next !== null) {
curr = curr.next;
}
// Update the previous node's next pointer
if (curr.prev !== null) {
curr.prev.next = null;
}
return head;
}
function printList(head) {
let curr = head;
while (curr !== null) {
console.log(curr.data + " ");
curr = curr.next;
}
}
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
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 = delLast(head);
printList(head);
Output
1 2
Time Complexity: O(n), where n is the number of nodes in the doubly linked list.
Auxiliary Space: O(1)