{"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>\u00a0<\/p>\n[pastacode lang=\u201dpython\u201d manual=\u201d%23%20Python%20program%20to%20delete%20a%20node%20from%20linked%20list%0A%20%0A%23%20Node%20class%20%0Aclass%20Node%3A%0A%20%0A%20%20%20%20%23%20Constructor%20to%20initialize%20the%20node%20object%0A%20%20%20%20def%20__init__(self%2C%20data)%3A%0A%20%20%20%20%20%20%20%20self.data%20%3D%20data%0A%20%20%20%20%20%20%20%20self.next%20%3D%20None%0A%20%0Aclass%20LinkedList%3A%0A%20%0A%20%20%20%20%23%20Function%20to%20initialize%20head%0A%20%20%20%20def%20__init__(self)%3A%0A%20%20%20%20%20%20%20%20self.head%20%3D%20None%0A%20%0A%20%20%20%20%23%20Function%20to%20insert%20a%20new%20node%20at%20the%20beginning%0A%20%20%20%20def%20push(self%2C%20new_data)%3A%0A%20%20%20%20%20%20%20%20new_node%20%3D%20Node(new_data)%0A%20%20%20%20%20%20%20%20new_node.next%20%3D%20self.head%0A%20%20%20%20%20%20%20%20self.head%20%3D%20new_node%0A%20%0A%20%20%20%20%23%20Given%20a%20reference%20to%20the%20head%20of%20a%20list%20and%20a%20key%2C%0A%20%20%20%20%23%20delete%20the%20first%20occurence%20of%20key%20in%20linked%20list%0A%20%20%20%20def%20deleteNode(self%2C%20key)%3A%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%23%20Store%20head%20node%0A%20%20%20%20%20%20%20%20temp%20%3D%20self.head%0A%20%0A%20%20%20%20%20%20%20%20%23%20If%20head%20node%20itself%20holds%20the%20key%20to%20be%20deleted%0A%20%20%20%20%20%20%20%20if%20(temp%20is%20not%20None)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(temp.data%20%3D%3D%20key)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20self.head%20%3D%20temp.next%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20temp%20%3D%20None%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%0A%20%0A%20%20%20%20%20%20%20%20%23%20Search%20for%20the%20key%20to%20be%20deleted%2C%20keep%20track%20of%20the%0A%20%20%20%20%20%20%20%20%23%20previous%20node%20as%20we%20need%20to%20change%20\u2019prev.next\u2019%0A%20%20%20%20%20%20%20%20while(temp%20is%20not%20None)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20temp.data%20%3D%3D%20key%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20break%0A%20%20%20%20%20%20%20%20%20%20%20%20prev%20%3D%20temp%0A%20%20%20%20%20%20%20%20%20%20%20%20temp%20%3D%20temp.next%0A%20%0A%20%20%20%20%20%20%20%20%23%20if%20key%20was%20not%20present%20in%20linked%20list%0A%20%20%20%20%20%20%20%20if(temp%20%3D%3D%20None)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%0A%20%0A%20%20%20%20%20%20%20%20%23%20Unlink%20the%20node%20from%20linked%20list%0A%20%20%20%20%20%20%20%20prev.next%20%3D%20temp.next%0A%20%0A%20%20%20%20%20%20%20%20temp%20%3D%20None%0A%20%0A%20%0A%20%20%20%20%23%20Utility%20function%20to%20print%20the%20linked%20LinkedList%0A%20%20%20%20def%20printList(self)%3A%0A%20%20%20%20%20%20%20%20temp%20%3D%20self.head%0A%20%20%20%20%20%20%20%20while(temp)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print%20%22%20%25d%22%20%25(temp.data)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20temp%20%3D%20temp.next%0A%20%0A%20%0A%23%20Driver%20program%0Allist%20%3D%20LinkedList()%0Allist.push(7)%0Allist.push(1)%0Allist.push(3)%0Allist.push(2)%0A%20%0Aprint%20%22Created%20Linked%20List%3A%20%22%0Allist.printList()%0Allist.deleteNode(1)%20%0Aprint%20%22%5CnLinked%20List%20after%20Deletion%20of%201%3A%22%0Allist.printList()%0A%20%0A%23%20This%20code%20is%20contributed%20by%20Nikhil%20Kumar%20Singh%20(nickzuck_007)%0A\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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=\u201dbanner\u201d]\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}]}}