{"id":26823,"date":"2017-06-01T14:12:47","date_gmt":"2017-06-01T08:42:47","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26823"},"modified":"2017-12-24T15:37:19","modified_gmt":"2017-12-24T10:07:19","slug":"26823-2","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/26823-2\/","title":{"rendered":"Python Algorithm &#8211; Deleting a node in Linked List | Set 3"},"content":{"rendered":"<p>We have discussed Linked List Introduction and Linked List Insertion in previous posts on singly linked list.<span id=\"more-142789\"><\/span><\/p>\n<p>Let us formulate the problem statement to understand the deletion process. <em>Given a \u2018key\u2019, delete the first occurrence of this key in linked list<\/em>.<br \/>\nTo delete a node from linked list, we need to do following steps.<br \/>\n1) Find previous node of the node to be deleted.<br \/>\n2) Changed next of previous node.<br \/>\n3) Free memory for the node to be deleted.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"alignleft size-full wp-image-26801\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Linkedlist_deletion-2.png\" alt=\"Deleting a node in Linked List | Set 3\" width=\"759\" height=\"243\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Linkedlist_deletion-2.png 759w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Linkedlist_deletion-2-300x96.png 300w\" sizes=\"(max-width: 759px) 100vw, 759px\" \/><\/p>\n<p>Since every node of linked list is dynamically allocated using malloc() in C, we need to call <a href=\"http:\/\/www.cplusplus.com\/reference\/cstdlib\/free\/\" target=\"_blank\" rel=\"noopener noreferrer\">free()<\/a> for freeing memory allocated for the node to be deleted.<\/p>\n<p><strong>Python Programming:<\/strong><\/p>\n<p>&nbsp;<\/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\"># Python program to delete a node from linked list<br\/> <br\/># Node class <br\/>class Node:<br\/> <br\/>    # Constructor to initialize the node object<br\/>    def __init__(self, data):<br\/>        self.data = data<br\/>        self.next = None<br\/> <br\/>class LinkedList:<br\/> <br\/>    # Function to initialize head<br\/>    def __init__(self):<br\/>        self.head = None<br\/> <br\/>    # Function to insert a new node at the beginning<br\/>    def push(self, new_data):<br\/>        new_node = Node(new_data)<br\/>        new_node.next = self.head<br\/>        self.head = new_node<br\/> <br\/>    # Given a reference to the head of a list and a key,<br\/>    # delete the first occurence of key in linked list<br\/>    def deleteNode(self, key):<br\/>         <br\/>        # Store head node<br\/>        temp = self.head<br\/> <br\/>        # If head node itself holds the key to be deleted<br\/>        if (temp is not None):<br\/>            if (temp.data == key):<br\/>                self.head = temp.next<br\/>                temp = None<br\/>                return<br\/> <br\/>        # Search for the key to be deleted, keep track of the<br\/>        # previous node as we need to change &#039;prev.next&#039;<br\/>        while(temp is not None):<br\/>            if temp.data == key:<br\/>                break<br\/>            prev = temp<br\/>            temp = temp.next<br\/> <br\/>        # if key was not present in linked list<br\/>        if(temp == None):<br\/>            return<br\/> <br\/>        # Unlink the node from linked list<br\/>        prev.next = temp.next<br\/> <br\/>        temp = None<br\/> <br\/> <br\/>    # Utility function to print the linked LinkedList<br\/>    def printList(self):<br\/>        temp = self.head<br\/>        while(temp):<br\/>            print &quot; %d&quot; %(temp.data),<br\/>            temp = temp.next<br\/> <br\/> <br\/># Driver program<br\/>llist = LinkedList()<br\/>llist.push(7)<br\/>llist.push(1)<br\/>llist.push(3)<br\/>llist.push(2)<br\/> <br\/>print &quot;Created Linked List: &quot;<br\/>llist.printList()<br\/>llist.deleteNode(1) <br\/>print &quot;\\nLinked List after Deletion of 1:&quot;<br\/>llist.printList()<br\/> <br\/># This code is contributed by Nikhil Kumar Singh (nickzuck_007)<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>Created Linked List:\r\n 2  3  1  7\r\nLinked List after Deletion of 1:\r\n 2  3  7<\/pre>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Python Algorithm &#8211; Deleting a node in Linked List &#8211; Linked List &#8211; We have discussed Linked List Introduction and Linked List Insertion in previous<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,79476,4148,79478],"tags":[79924,79917,79920,79922,79925,79918,79914,79916,79919,79921,79915,79923],"class_list":["post-26823","post","type-post","status-publish","format-standard","hentry","category-coding","category-linked-list","category-python","category-singly-linked-list","tag-c-program-to-delete-first-node-in-linked-list","tag-delete-a-node-from-linked-list-algorithm","tag-delete-a-node-in-linked-list-with-single-pointer","tag-delete-a-node-in-singly-linked-list","tag-delete-a-node-in-the-middle-of-a-singly-linked-list","tag-delete-last-node-in-linked-list-in-c","tag-delete-node-at-given-position-in-a-linked-list","tag-delete-node-linked-list-c","tag-delete-specific-node-in-linked-list","tag-deletion-in-linked-list-in-data-structure","tag-linked-list-delete-node-java","tag-write-a-program-to-delete-a-node-from-linked-list-in-c"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26823","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=26823"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26823\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26823"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26823"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26823"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}