Open In App

Javascript Program For Deleting A Node In A Doubly Linked List

Last Updated : 02 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Pre-requisite: Doubly Link List Set 1| Introduction and Insertion

Write a function to delete a given node in a doubly-linked list. 

Original Doubly Linked List 

Approach: The deletion of a node in a doubly-linked list can be divided into three main categories: 

  • After the deletion of the head node. 
  • After the deletion of the middle node. 
  • After the deletion of the last node.

All three mentioned cases can be handled in two steps if the pointer of the node to be deleted and the head pointer is known. 

  1. If the node to be deleted is the head node then make the next node as head.
  2. If a node is deleted, connect the next and previous node of the deleted node.

Algorithm 

  • Let the node to be deleted be del.
  • If node to be deleted is head node, then change the head pointer to next current head.
if headnode == del then
headnode = del.nextNode
  • Set next of previous to del, if previous to del exists.
if del.nextNode != none 
del.nextNode.previousNode = del.previousNode
  • Set prev of next to del, if next to del exists.
if del.previousNode != none 
del.previousNode.nextNode = del.next

Output
Created DLL is: 
10 
8 
4 
2 
Modified Linked list: 
8 

Complexity Analysis: 

  • Time Complexity: O(1). 
    Since traversal of the linked list is not required so the time complexity is constant.
  • Space Complexity: O(1). 
    As no extra space is required, so the space complexity is constant.

Please refer complete article on Delete a node in a Doubly Linked List for more details!


Next Article

Similar Reads