Open In App

Deletion in a Doubly Linked List

Last Updated : 04 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

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 <-> 3

Input: DLL = 2 <-> 45 <-> 3 <-> 1, Node = 45
Output:  2 <-> 3<-> 1

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

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 headhead = 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++
// 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 Java Python C# JavaScript

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

1

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++
// 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 Java Python C# JavaScript

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

2

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++
// 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 Java Python C# JavaScript

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

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++
// 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 Java Python C# JavaScript

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-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 NULLcurr->prev->next = NULL.
  • Free the memory allocated for the node that was deleted.

Below is the implementation of the above approach: 

C++
// 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 Java Python C# JavaScript

Output
1 2 

Time Complexity: O(n), where n is the number of nodes in the doubly linked list.
Auxiliary Space: O(1)



Next Article
Practice Tags :

Similar Reads

three90RightbarBannerImg