{"id":27062,"date":"2018-01-02T21:40:41","date_gmt":"2018-01-02T16:10:41","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27062"},"modified":"2018-01-02T21:40:41","modified_gmt":"2018-01-02T16:10:41","slug":"write-function-delete-linked-list","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/write-function-delete-linked-list\/","title":{"rendered":"C Algorithm &#8211; Write a function to delete a Linked List"},"content":{"rendered":"<p><strong>Algorithm For C\/C++:<\/strong> Iterate through the linked list and delete all the nodes one by one. Main point here is not to access next of the current pointer if current pointer is deleted.<\/p>\n<p>In <strong>Java<\/strong>, automatic garbage collection happens, so deleting a linked list is easy. We just need to change head to null.<\/p>\n<p><strong>C Programming:<\/strong><\/p>\n[pastacode lang=\u201dc\u201d manual=\u201d%2F%2F%20C%20program%20to%20delete%20a%20linked%20list%0A%23include%3Cstdio.h%3E%0A%23include%3Cstdlib.h%3E%0A%23include%3Cassert.h%3E%0A%20%0A%2F*%20Link%20list%20node%20*%2F%0Astruct%20node%0A%7B%0A%20%20%20%20int%20data%3B%0A%20%20%20%20struct%20node*%20next%3B%0A%7D%3B%0A%20%0A%2F*%20Function%20to%20delete%20the%20entire%20linked%20list%20*%2F%0Avoid%20deleteList(struct%20node**%20head_ref)%0A%7B%0A%20%20%20%2F*%20deref%20head_ref%20to%20get%20the%20real%20head%20*%2F%0A%20%20%20struct%20node*%20current%20%3D%20*head_ref%3B%0A%20%20%20struct%20node*%20next%3B%0A%20%0A%20%20%20while%20(current%20!%3D%20NULL)%20%0A%20%20%20%7B%0A%20%20%20%20%20%20%20next%20%3D%20current-%3Enext%3B%0A%20%20%20%20%20%20%20free(current)%3B%0A%20%20%20%20%20%20%20current%20%3D%20next%3B%0A%20%20%20%7D%0A%20%20%20%0A%20%20%20%2F*%20deref%20head_ref%20to%20affect%20the%20real%20head%20back%0A%20%20%20%20%20%20in%20the%20caller.%20*%2F%0A%20%20%20*head_ref%20%3D%20NULL%3B%0A%7D%0A%20%0A%2F*%20Given%20a%20reference%20(pointer%20to%20pointer)%20to%20the%20head%0A%20%20of%20a%20list%20and%20an%20int%2C%20push%20a%20new%20node%20on%20the%20front%0A%20%20of%20the%20list.%20*%2F%0Avoid%20push(struct%20node**%20head_ref%2C%20int%20new_data)%0A%7B%0A%20%20%20%20%2F*%20allocate%20node%20*%2F%0A%20%20%20%20struct%20node*%20new_node%20%3D%0A%20%20%20%20%20%20%20%20%20%20%20%20(struct%20node*)%20malloc(sizeof(struct%20node))%3B%0A%20%0A%20%20%20%20%2F*%20put%20in%20the%20data%20%20*%2F%0A%20%20%20%20new_node-%3Edata%20%20%3D%20new_data%3B%0A%20%20%20%20%0A%20%20%20%20%2F*%20link%20the%20old%20list%20off%20the%20new%20node%20*%2F%0A%20%20%20%20new_node-%3Enext%20%3D%20(*head_ref)%3B%0A%20%20%20%20%0A%20%20%20%20%2F*%20move%20the%20head%20to%20point%20to%20the%20new%20node%20*%2F%0A%20%20%20%20(*head_ref)%20%20%20%20%3D%20new_node%3B%0A%7D%0A%20%0A%2F*%20Drier%20program%20to%20test%20count%20function*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20%2F*%20Start%20with%20the%20empty%20list%20*%2F%0A%20%20%20%20struct%20node*%20head%20%3D%20NULL%3B%0A%20%20%20%20%0A%20%20%20%20%2F*%20Use%20push()%20to%20construct%20below%20list%0A%20%20%20%20%201-%3E12-%3E1-%3E4-%3E1%20%20*%2F%0A%20%20%20%20push(%26head%2C%201)%3B%0A%20%20%20%20push(%26head%2C%204)%3B%0A%20%20%20%20push(%26head%2C%201)%3B%20%0A%20%20%20%20push(%26head%2C%2012)%3B%0A%20%20%20%20push(%26head%2C%201)%3B%20%20%20%0A%20%20%20%20%0A%20%20%20%20printf(%22%5Cn%20Deleting%20linked%20list%22)%3B%0A%20%20%20%20deleteList(%26head)%3B%20%20%0A%20%20%20%20%0A%20%20%20%20printf(%22%5Cn%20Linked%20list%20deleted%22)%3B%0A%7D\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Output:<\/strong><\/p>\n<pre> Deleting linked list\r\n Linked list deleted<\/pre>\n<p><strong>Time Complexity:<\/strong> O(n)<br \/>\n<strong>Auxiliary Space:<\/strong> O(1)<\/p>\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>C Algorithm &#8211; Write a function to delete a Linked List &#8211; Linked List &#8211;  Iterate through the linked list and delete all the nodes one by one.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69866,1,79476,79478],"tags":[79925,79918,79914,79921,80734,80733,79915,79923,80735],"class_list":["post-27062","post","type-post","status-publish","format-standard","hentry","category-c-programming","category-coding","category-linked-list","category-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-deletion-in-linked-list-in-data-structure","tag-destructor-linked-list-c","tag-linked-list-delete-java","tag-linked-list-delete-node-java","tag-write-a-program-to-delete-a-node-from-linked-list-in-c","tag-write-an-algorithm-to-delete-an-element-from-linked-list"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27062","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=27062"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27062\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27062"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27062"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27062"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}