{"id":27339,"date":"2018-01-20T21:14:48","date_gmt":"2018-01-20T15:44:48","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27339"},"modified":"2018-01-20T21:14:48","modified_gmt":"2018-01-20T15:44:48","slug":"doubly-linked-list","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/doubly-linked-list\/","title":{"rendered":"Delete a node in a Doubly Linked List"},"content":{"rendered":"<p>Write a function to delete a given node in a doubly linked list.<span id=\"more-7293\"><\/span><\/p>\n<pre>     (a) Original Doubly Linked List<\/pre>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-27303\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/DLL3-1.png\" alt=\"Delete a node in a Doubly Linked List\" width=\"553\" height=\"112\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/DLL3-1.png 553w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/DLL3-1-300x61.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/DLL3-1-550x112.png 550w\" sizes=\"(max-width: 553px) 100vw, 553px\" \/><\/p>\n<pre>     (a) After deletion of head node<\/pre>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-27275\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/DLL1-1.png\" alt=\"Delete a node in a Doubly Linked List\" width=\"423\" height=\"112\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/DLL1-1.png 423w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/DLL1-1-300x79.png 300w\" sizes=\"(max-width: 423px) 100vw, 423px\" \/><\/p>\n<pre>     (a) After deletion of middle node<\/pre>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-27278\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/DLL2.png\" alt=\"Delete a node in a Doubly Linked List\" width=\"323\" height=\"112\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/DLL2.png 323w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/DLL2-300x104.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/DLL2-320x112.png 320w\" sizes=\"(max-width: 323px) 100vw, 323px\" \/><\/p>\n<pre>     (a) After deletion of last node<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-27279\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/DLL0.png\" alt=\"Delete a node in a Doubly Linked List\" width=\"195\" height=\"112\" \/><\/p>\n<div id=\"practice\">\n<h4 id=\"algorithmlet-the-node-to-be-deleted-is-del-1-if-node-to-be-deleted-is-head-node-then-change-the-head-pointer-to-next-current-head-2-set-next-of-previous-to-del-if-previous-to-del-exixts-3-se\">Algorithm:<br \/>\nLet the node to be deleted is <em>del<\/em>.<br \/>\n1) If node to be deleted is head node, then change the head pointer to next current head.<br \/>\n2) Set <em>next <\/em>of previous to <em>del<\/em>, if previous to <em>del<\/em> exixts.<br \/>\n3) Set <em>prev <\/em>of next to <em>del<\/em>, if next to <em>del<\/em> exixts.<\/h4>\n[ad type=&#8221;banner&#8221;]\n<p>Python Programming:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\"># Program to delete a node in doubly linked list<br\/> <br\/># for Garbage collection<br\/>import gc<br\/> <br\/># A node of the doublly linked list<br\/>class Node:<br\/>     <br\/>    # Constructor to create a new node<br\/>    def __init__(self, data):<br\/>        self.data = data <br\/>        self.next = None<br\/>        self.prev = None<br\/> <br\/>class DoublyLinkedList:<br\/>     # Constructor for empty Doubly Linked List<br\/>    def __init__(self):<br\/>        self.head = None<br\/>  <br\/>   # Function to delete a node in a Doubly Linked List.<br\/>   # head_ref --&gt; pointer to head node pointer.<br\/>   # dele --&gt; pointer to node to be deleted<br\/> <br\/>    def deleteNode(self, dele):<br\/>         <br\/>        # Base Case<br\/>        if self.head is None or dele is None:<br\/>            return<br\/>         <br\/>        # If node to be deleted is head node<br\/>        if self.head == dele:<br\/>            self.head = dele.next<br\/> <br\/>        # Change next only if node to be deleted is NOT<br\/>        # the last node<br\/>        if dele.next is not None:<br\/>            dele.next.prev = dele.prev<br\/>     <br\/>        # Change prev only if node to be deleted is NOT <br\/>        # the first node<br\/>        if dele.prev is not None:<br\/>            dele.prev.next = dele.next<br\/>        # Finally, free the memory occupied by dele<br\/>        # Call python garbage collector<br\/>        gc.collect()<br\/> <br\/> <br\/>    # Given a reference to the head of a list and an<br\/>    # integer,inserts a new node on the front of list<br\/>    def push(self, new_data):<br\/>  <br\/>        # 1. Allocates node<br\/>        # 2. Put the data in it<br\/>        new_node = Node(new_data)<br\/>  <br\/>        # 3. Make next of new node as head and<br\/>        # previous as None (already None)<br\/>        new_node.next = self.head<br\/>  <br\/>        # 4. change prev of head node to new_node<br\/>        if self.head is not None:<br\/>            self.head.prev = new_node<br\/>  <br\/>        # 5. move the head to point to the new node<br\/>        self.head = new_node<br\/> <br\/> <br\/>    def printList(self, node):<br\/>        while(node is not None):<br\/>            print node.data,<br\/>            node = node.next<br\/> <br\/> <br\/># Driver program to test the above functions<br\/> <br\/># Start with empty list<br\/>dll = DoublyLinkedList()<br\/> <br\/># Let us create the doubly linked list 10&lt;-&gt;8&lt;-&gt;4&lt;-&gt;2<br\/>dll.push(2);<br\/>dll.push(4);<br\/>dll.push(8);<br\/>dll.push(10);<br\/> <br\/>print &quot;\\n Original Linked List&quot;,<br\/>dll.printList(dll.head)<br\/> <br\/># delete nodes from doubly linked list<br\/>dll.deleteNode(dll.head)<br\/>dll.deleteNode(dll.head.next)<br\/>dll.deleteNode(dll.head.next)<br\/># Modified linked list will be NULL&lt;-8-&gt;NULL<br\/>print &quot;\\n Modified Linked List&quot;,<br\/>dll.printList(dll.head)<br\/> <\/code><\/pre> <\/div>\n<p>Time Complexity: O(1)<br \/>\nTime Complexity: O(1)<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Delete a node in a Doubly Linked List &#8211; Doubly Linked List &#8211; If node to be deleted is head node, then change the head pointer to next current head.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,79479,79476],"tags":[73376,81324,81321,81320,81323,81318,81322,81319],"class_list":["post-27339","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-doubly-linked-list","category-linked-list","tag-algorithm-for-insertion-and-deletion-in-doubly-linked-list","tag-delete-all-nodes-in-doubly-linked-list","tag-delete-last-node-in-doubly-linked-list-in-c","tag-delete-node-from-doubly-linked-list-java","tag-doubly-linked-list-c-examples","tag-insertion-and-deletion-in-doubly-linked-list-in-c","tag-insertion-and-deletion-in-doubly-linked-list-in-data-structure","tag-write-a-function-to-delete-a-node-from-doubly-linked-list"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27339","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=27339"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27339\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27339"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27339"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27339"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}