{"id":27061,"date":"2018-01-02T21:40:01","date_gmt":"2018-01-02T16:10:01","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27061"},"modified":"2018-01-02T21:40:01","modified_gmt":"2018-01-02T16:10:01","slug":"c-algorithm-write-function-delete-linked-list","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-algorithm-write-function-delete-linked-list\/","title":{"rendered":"Cpp 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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">\/\/ C program to delete a linked list<br\/>#include&lt;stdio.h&gt;<br\/>#include&lt;stdlib.h&gt;<br\/>#include&lt;assert.h&gt;<br\/> <br\/>\/* Link list node *\/<br\/>struct node<br\/>{<br\/>    int data;<br\/>    struct node* next;<br\/>};<br\/> <br\/>\/* Function to delete the entire linked list *\/<br\/>void deleteList(struct node** head_ref)<br\/>{<br\/>   \/* deref head_ref to get the real head *\/<br\/>   struct node* current = *head_ref;<br\/>   struct node* next;<br\/> <br\/>   while (current != NULL) <br\/>   {<br\/>       next = current-&gt;next;<br\/>       free(current);<br\/>       current = next;<br\/>   }<br\/>   <br\/>   \/* deref head_ref to affect the real head back<br\/>      in the caller. *\/<br\/>   *head_ref = NULL;<br\/>}<br\/> <br\/>\/* Given a reference (pointer to pointer) to the head<br\/>  of a list and an int, push a new node on the front<br\/>  of the list. *\/<br\/>void push(struct node** head_ref, int new_data)<br\/>{<br\/>    \/* allocate node *\/<br\/>    struct node* new_node =<br\/>            (struct node*) malloc(sizeof(struct node));<br\/> <br\/>    \/* put in the data  *\/<br\/>    new_node-&gt;data  = new_data;<br\/>    <br\/>    \/* link the old list off the new node *\/<br\/>    new_node-&gt;next = (*head_ref);<br\/>    <br\/>    \/* move the head to point to the new node *\/<br\/>    (*head_ref)    = new_node;<br\/>}<br\/> <br\/>\/* Drier program to test count function*\/<br\/>int main()<br\/>{<br\/>    \/* Start with the empty list *\/<br\/>    struct node* head = NULL;<br\/>    <br\/>    \/* Use push() to construct below list<br\/>     1-&gt;12-&gt;1-&gt;4-&gt;1  *\/<br\/>    push(&amp;head, 1);<br\/>    push(&amp;head, 4);<br\/>    push(&amp;head, 1); <br\/>    push(&amp;head, 12);<br\/>    push(&amp;head, 1);   <br\/>    <br\/>    printf(&quot;\\n Deleting linked list&quot;);<br\/>    deleteList(&amp;head);  <br\/>    <br\/>    printf(&quot;\\n Linked list deleted&quot;);<br\/>}<\/code><\/pre> <\/div>\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=&#8221;banner&#8221;]\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":[83515,1,79476,79478],"tags":[79917,80732,79922,79925,79918,79914,79921,80734,80733,79915,79923,80735],"class_list":["post-27061","post","type-post","status-publish","format-standard","hentry","category-c-programming-3","category-coding","category-linked-list","category-singly-linked-list","tag-delete-a-node-from-linked-list-algorithm","tag-delete-a-node-from-linked-list-using-one-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-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\/27061","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=27061"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27061\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27061"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27061"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27061"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}