{"id":27433,"date":"2018-02-02T21:28:55","date_gmt":"2018-02-02T15:58:55","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27433"},"modified":"2018-02-02T21:28:55","modified_gmt":"2018-02-02T15:58:55","slug":"python-algorithm-delete-a-linked-list-node-at-a-given-position","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/python-algorithm-delete-a-linked-list-node-at-a-given-position\/","title":{"rendered":"Python Algorithm- Delete a Linked List node at a given position"},"content":{"rendered":"<p>Given a singly linked list and a position, delete a linked list node at the given position.<span id=\"more-142796\"><\/span><\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre>Input: position = 1, Linked List = 8->2->3->1->7\r\nOutput: Linked List =  8->3->1->7\r\n\r\nInput: position = 0, Linked List = 8->2->3->1->7\r\nOutput: Linked List = 2->3->1->7<\/pre>\n<p>If node to be deleted is root, simply delete it. To delete a middle node, we must have pointer to the node previous to the node to be deleted. So if positions is not zero, we run a loop position-1 times and get pointer to the previous node.<\/p>\n[ad type=\u201dbanner\u201d]\n<p><strong>Python Programming:<\/strong><\/p>\n[pastacode lang=\u201dpython\u201d manual=\u201d%23%20Python%20program%20to%20delete%20a%20node%20in%20a%20linked%20list%0A%23%20at%20a%20given%20position%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%20Constructor%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%20%0A%20%20%20%20%23%20and%20a%20position%2C%20delete%20the%20node%20at%20a%20given%20position%0A%20%20%20%20def%20deleteNode(self%2C%20position)%3A%0A%20%0A%20%20%20%20%20%20%20%20%23%20If%20linked%20list%20is%20empty%0A%20%20%20%20%20%20%20%20if%20self.head%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%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%20needs%20to%20be%20removed%0A%20%20%20%20%20%20%20%20if%20position%20%3D%3D%200%3A%0A%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%20temp%20%3D%20None%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%20Find%20previous%20node%20of%20the%20node%20to%20be%20deleted%0A%20%20%20%20%20%20%20%20for%20i%20in%20range(position%20-1%20)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20temp%20%3D%20temp.next%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20temp%20is%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20break%0A%20%0A%20%20%20%20%20%20%20%20%23%20If%20position%20is%20more%20than%20number%20of%20nodes%0A%20%20%20%20%20%20%20%20if%20temp%20is%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%0A%20%20%20%20%20%20%20%20if%20temp.next%20is%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%20Node%20temp.next%20is%20the%20node%20to%20be%20deleted%0A%20%20%20%20%20%20%20%20%23%20store%20pointer%20to%20the%20next%20of%20node%20to%20be%20deleted%0A%20%20%20%20%20%20%20%20next%20%3D%20temp.next.next%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%20temp.next%20%3D%20None%0A%20%0A%20%20%20%20%20%20%20%20temp.next%20%3D%20next%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%20%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%20to%20test%20above%20function%0Allist%20%3D%20LinkedList()%0Allist.push(7)%0Allist.push(1)%0Allist.push(3)%0Allist.push(2)%0Allist.push(8)%0A%20%0Aprint%20%22Created%20Linked%20List%3A%20%22%0Allist.printList()%0Allist.deleteNode(4)%0Aprint%20%22%5CnLinked%20List%20after%20Deletion%20at%20position%204%3A%20%22%0Allist.printList()%0A%20\u2033 message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Output:<\/strong><\/p>\n<pre>Created Linked List: \r\n 8  2  3  1  7 \r\nLinked List after Deletion at position 4: \r\n 8  2  3  1<\/pre>\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>Python Algorithm- Delete a Linked List node at a given position &#8211; Linked List If node to be deleted is root, simply delete it. To delete a middle node<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79476,79478],"tags":[79924,79917,79920,79922,79918,79914,79916,79921,79915,79923],"class_list":["post-27433","post","type-post","status-publish","format-standard","hentry","category-linked-list","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-last-node-in-linked-list-in-c","tag-delete-node-at-given-position-in-a-linked-list","tag-delete-node-linked-list-c","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\/27433","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=27433"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27433\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}