# Deletion in Doubly Linked List

Nodes can be removed from the double link list from any location such as front, end, or any other specific location or specific data.

When you remove a node from the list of double links, first relocate the pointer to that particular node so that the previous and next nodes do not connect to the node you want to remove. Then you can easily remove the node.

Consider the following list of double-linked lists with three nodes A, B, and C. Suppose you need to remove node B.

We have shown that node B will be removed from the specified linked list, as shown in the series of figures above. The order of operations remains the same whether the node is first or last. The only thing to note is that if the first node is removed, the pointer before the second node will be set to null.

Similarly, when the last node is removed, the next pointer to the previous node is set to null. If you remove the nodes between the nodes, the sequence will look like the one above.

#### Here Code:

```#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;
}```

### Deletion operation:

The delete operation removes the node from a specific location in the double-link list.

• Deletion of the first node: Deletes the first node in the list.
• Deletion of the last node: Deletes the last node in the list.
• Deletion of a node given the data: When data is specified, the operation matches the data with the data of the node in the linked list and deletes the node.

#### Here Code:

```#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node* next;
Node* prev;
};
{
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;
}
{
Node* new_node = new Node();
new_node->data = new_data;
new_node->prev = NULL;
}
void printList(Node* node)
{
while (node != NULL)
{
cout << node->data << " ";
node = node->next;
}
}
int main()
{
cout << "Original Linked list ";
/*delete first node*/
/*delete middle node*/
/*delete last node*/
cout << "\nModified Linked list ";
return 0;
}```

### Complexity Analysis:

• Time Complexity: O(1): Time complexity is constant because the linked list is not required to be scanned.
• Space Complexity: O(1): Spatial complexity is constant because no additional space is needed.

### References:

Help Me Please: If you found any error in the content or example please let us know by comment also write your suggestion or if any doubt in the concept please feel free to write us in the comment section.

## Practice your Code online here

(Visited 113 times, 1 visits today)

Article Rating
Subscribe
Notify of
1 Comment
Inline Feedbacks

[…] Deletion in Doubly Linked List […]