# Delete a node in a Doubly Linked List in C++

In this tutorial, we are going to learn how to delete a node in doubly linked list.

Let's see the steps to solve the problem.

• Write struct with data, prev and next pointers.

• Write a function to insert the node into the doubly linked list.

• Initialize the doubly linked list with dummy data.

• Take a node to delete.

• Write a function to delete the node. Consider the following three cases while deleting the node.

• If the node is head node, then move the head to next node.

• If the node is middle node, then link the next node to the previous node

• If the node is end node, then remove the previous node link. Let's see the code.

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node *prev, *next;
};
void deleteNode(Node** head_ref, Node* del) {
if (*head_ref == NULL || del == NULL) {
return;
}
}
if (del->next != NULL) {
del->next->prev = del->prev;
}
if (del->prev != NULL) {
del->prev->next = del->next;
}
free(del);
return;
}
void insertNode(Node** head_ref, int new_data) {
Node* new_node = new Node();
new_node->data = new_data;
new_node->prev = NULL;
}
}
while (node != NULL) {
cout << node->data << " -> ";
node = node->next;
}
}
int main() {
cout << "Linked List before deletion:" << endl;
cout << "\nLinked List after deletion:" << endl;
return 0;
}

## Output

If you execute the above program, then you will get the following result.

Linked List before deletion:
5 -> 4 -> 3 -> 2 -> 1 ->
5 -> 3 -> 2 -> 1 ->