{"id":27060,"date":"2018-01-02T21:35:14","date_gmt":"2018-01-02T16:05:14","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27060"},"modified":"2018-01-02T21:35:14","modified_gmt":"2018-01-02T16:05:14","slug":"java-algorithm-write-function-delete-linked-list","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/java-algorithm-write-function-delete-linked-list\/","title":{"rendered":"Java 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>Java Programming:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">\/\/ Java program to delete a linked list<br\/>class LinkedList<br\/>{<br\/>    Node head; \/\/ head of the list<br\/> <br\/>    \/* Linked List node *\/<br\/>    class Node<br\/>    {<br\/>        int data;<br\/>        Node next;<br\/>        Node(int d) { data = d; next = null; }<br\/>    }<br\/> <br\/>    \/* Function deletes the entire linked list *\/<br\/>    void deleteList()<br\/>    {<br\/>        head = null;<br\/>    }<br\/> <br\/>    \/* Inserts a new Node at front of the list. *\/<br\/>    public void push(int new_data)<br\/>    {<br\/>        \/* 1 &amp; 2: Allocate the Node &amp;<br\/>                  Put in the data*\/<br\/>        Node new_node = new Node(new_data);<br\/> <br\/>        \/* 3. Make next of new Node as head *\/<br\/>        new_node.next = head;<br\/> <br\/>        \/* 4. Move the head to point to new Node *\/<br\/>        head = new_node;<br\/>    }<br\/> <br\/>    public static void main(String [] args)<br\/>    {<br\/>        LinkedList llist = new LinkedList();<br\/>        \/* Use push() to construct below list<br\/>           1-&gt;12-&gt;1-&gt;4-&gt;1  *\/<br\/> <br\/>        llist.push(1);<br\/>        llist.push(4);<br\/>        llist.push(1);<br\/>        llist.push(12);<br\/>        llist.push(1);<br\/> <br\/>        System.out.println(&quot;Deleting the list&quot;);<br\/>        llist.deleteList();<br\/> <br\/>        System.out.println(&quot;Linked list deleted&quot;);<br\/>    }<br\/>}<br\/>\/\/ This code is contributed by Rajat Mishra<\/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>Java 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":[79476,79478],"tags":[79917,80732,79922,79925,79918,79914,79921,80734,80733,79915,79923,80735],"class_list":["post-27060","post","type-post","status-publish","format-standard","hentry","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\/27060","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=27060"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27060\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27060"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27060"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27060"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}