{"id":27334,"date":"2018-01-20T20:59:16","date_gmt":"2018-01-20T15:29:16","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27334"},"modified":"2018-01-20T20:59:41","modified_gmt":"2018-01-20T15:29:41","slug":"delete-a-node-in-a-doubly-linked-list-doubly-linked-list","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/delete-a-node-in-a-doubly-linked-list-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<\/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-27277\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/DLL3.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.png 553w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/DLL3-300x61.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/DLL3-550x112.png 550w\" sizes=\"(max-width: 553px) 100vw, 553px\" \/><\/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<p>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.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p>Java Programming:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">\/\/ Java program to delete a node from doubly linked list<br\/> <br\/>class LinkedList {<br\/> <br\/>    static Node head = null;<br\/> <br\/>    class Node {<br\/> <br\/>        int data;<br\/>        Node next, prev;<br\/> <br\/>        Node(int d) {<br\/>            data = d;<br\/>            next = prev = null;<br\/>        }<br\/>    }<br\/> <br\/>    \/*Function to delete a node in a Doubly Linked List.<br\/>    head_ref --&gt; pointer to head node pointer.<br\/>    del  --&gt;  pointer to node to be deleted. *\/<br\/>    void deleteNode(Node head_ref, Node del) {<br\/> <br\/>        \/* base case *\/<br\/>        if (head == null || del == null) {<br\/>            return;<br\/>        }<br\/> <br\/>        \/* If node to be deleted is head node *\/<br\/>        if (head == del) {<br\/>            head = del.next;<br\/>        }<br\/> <br\/>        \/* Change next only if node to be deleted is NOT the last node *\/<br\/>        if (del.next != null) {<br\/>            del.next.prev = del.prev;<br\/>        }<br\/> <br\/>        \/* Change prev only if node to be deleted is NOT the first node *\/<br\/>        if (del.prev != null) {<br\/>            del.prev.next = del.next;<br\/>        }<br\/> <br\/>        \/* Finally, free the memory occupied by del*\/<br\/>        return;<br\/>    }<br\/> <br\/>    \/* UTILITY FUNCTIONS *\/<br\/>    \/* Function to insert a node at the beginning of the Doubly Linked List *\/<br\/>    void push(Node head_ref, int new_data) {<br\/> <br\/>        \/* allocate node *\/<br\/>        Node new_node = new Node(new_data);<br\/> <br\/>        \/* since we are adding at the begining,<br\/>         prev is always NULL *\/<br\/>        new_node.prev = null;<br\/> <br\/>        \/* link the old list off the new node *\/<br\/>        new_node.next = (head);<br\/> <br\/>        \/* change prev of head node to new node *\/<br\/>        if ((head) != null) {<br\/>            (head).prev = new_node;<br\/>        }<br\/> <br\/>        \/* move the head to point to the new node *\/<br\/>        (head) = new_node;<br\/>    }<br\/> <br\/>     \/*Function to print nodes in a given doubly linked list<br\/>     This function is same as printList() of singly linked lsit *\/<br\/>    void printList(Node node) {<br\/>        while (node != null) {<br\/>            System.out.print(node.data + &quot; &quot;);<br\/>            node = node.next;<br\/>        }<br\/>    }<br\/> <br\/>    public static void main(String[] args) {<br\/>        LinkedList list = new LinkedList();<br\/> <br\/>        \/* Let us create the doubly linked list 10&lt;-&gt;8&lt;-&gt;4&lt;-&gt;2 *\/<br\/>        list.push(head, 2);<br\/>        list.push(head, 4);<br\/>        list.push(head, 8);<br\/>        list.push(head, 10);<br\/> <br\/>        System.out.println(&quot;Original Linked list &quot;);<br\/>        list.printList(head);<br\/> <br\/>        \/* delete nodes from the doubly linked list *\/<br\/>        list.deleteNode(head, head);  \/*delete first node*\/<br\/> <br\/>        list.deleteNode(head, head.next);  \/*delete middle node*\/<br\/> <br\/>        list.deleteNode(head, head.next);  \/*delete last node*\/<br\/>        System.out.println(&quot;&quot;);<br\/> <br\/>        \/* Modified linked list will be NULL&lt;-8-&gt;NULL *\/<br\/>        System.out.println(&quot;Modified Linked List&quot;);<br\/>        list.printList(head);<br\/>    }<br\/>}<\/code><\/pre> <\/div>\n<p>Time Complexity: O(1)<br \/>\nTime Complexity: O(1)<\/p>\n<div id=\"company_tags\">\u00a0[ad type=&#8221;banner&#8221;]<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Delete a node in a 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":[79479,79476],"tags":[73376,81324,81321,81320,81323,81318,81322,81319],"class_list":["post-27334","post","type-post","status-publish","format-standard","hentry","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\/27334","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=27334"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27334\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}