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.
- If the node to be deleted is the head node then make the next node as head.
- 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
Javascript
<script> // Javascript program to delete a node from // Doubly Linked List // Class for Doubly Linked List // Head of list var head; // Doubly Linked list Node class Node { // Constructor to create a new node // next and prev is by default // initialized as null constructor(val) { this .data = val; this .prev = null ; this .next = null ; } } // Adding a node at the front of the list function push(new_data) { // 1. allocate node // 2. put in the data new_Node = new Node(new_data); // 3. Make next of new node as head // and previous as NULL new_Node.next = head; new_Node.prev = null ; // 4. change prev of head node to // new node if (head != null ) head.prev = new_Node; // 5. move the head to point to the // new node head = new_Node; } // This function prints contents of // linked list starting from the given node function printlist(node) { last = null ; while (node != null ) { document.write(node.data + " " ); last = node; node = node.next; } document.write( "<br/>" ); } // Function to delete a node in a Doubly // Linked List. head_ref --> pointer to // head node pointer. del --> data of // node to be deleted. function deleteNode(del) { // Base case if (head == null || del == null ) { return ; } // If node to be deleted is head node if (head == del) { head = del.next; } // Change next only if node to be // deleted is NOT the last node if (del.next != null ) { del.next.prev = del.prev; } // Change prev only if node to be // deleted is NOT the first node if (del.prev != null ) { del.prev.next = del.next; } // Finally, free the memory occupied // by del return ; } // Driver Code // Start with the empty list // Insert 2. So linked list becomes // 2->NULL push(2); // Insert 4. So linked list becomes // 4->2->NULL push(4); // Insert 8. So linked list becomes // 8->4->2->NULL push(8); // Insert 10. So linked list becomes // 10->8->4->2->NULL push(10); document.write( "Created DLL is: " ); printlist(head); // Deleting first node deleteNode(head); deleteNode(head.next); deleteNode(head.next); document.write( "Modified Linked list: " ); printlist(head); // This code is contributed by todaysgaurav </script> |
Output:
Original Linked list 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!