{"id":27695,"date":"2018-04-09T20:22:50","date_gmt":"2018-04-09T14:52:50","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27695"},"modified":"2018-10-27T16:46:41","modified_gmt":"2018-10-27T11:16:41","slug":"java-algorithm-delete-alternate-nodes-linked-list","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/java-algorithm-delete-alternate-nodes-linked-list\/","title":{"rendered":"Delete alternate nodes of a Linked List"},"content":{"rendered":"<p>Given a <a href=\"https:\/\/www.wikitechy.com\/technology\/insertion-sort-singly-linked-list\/\" target=\"_blank\" rel=\"noopener\">Singly Linked List<\/a>, starting from the second node delete all alternate nodes of it.<\/p>\n<p><strong>For example<\/strong>, if the given linked list is 1-&gt;2-&gt;3-&gt;4-&gt;5 then your function should convert it to 1-&gt;3-&gt;5, and<\/p>\n<p>if the given <a href=\"https:\/\/www.wikitechy.com\/tutorials\/java\/java-linked-list\" target=\"_blank\" rel=\"noopener\">linked list<\/a> is 1-&gt;2-&gt;3-&gt;4 then convert it to 1-&gt;3.<span id=\"more-7443\"><\/span><\/p>\n<p>In <a href=\"https:\/\/www.wikitechy.com\/tutorials\/java\/what-is-java\" target=\"_blank\" rel=\"noopener\">java programming<\/a>, the solution is given below<\/p>\n<h3 id=\"method-1-iterative\"><span style=\"color: #003366;\"><strong>Method 1 (Iterative):<\/strong><\/span><\/h3>\n<p>Keep track of previous of the node to be deleted. First change the next link of previous node and then free the memory allocated for the node<\/p>\n<h4 id=\"java-programming-delete-alternate-nodes-of-linked-list\"><span style=\"color: #333399;\"><strong>Java Programming: Delete alternate nodes of linked list:<\/strong><\/span><\/h4>\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 alternate nodes of a linked list<br\/>class LinkedList<br\/>{<br\/>    Node head;  \/\/ head of 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\/>    void deleteAlt()<br\/>    {<br\/>       if (head == null) <br\/>          return;<br\/> <br\/>       Node prev = head;<br\/>       Node now = head.next;<br\/> <br\/>       while (prev != null &amp;&amp; now != null) <br\/>       {           <br\/>           \/* Change next link of previus node *\/<br\/>           prev.next = now.next;<br\/> <br\/>           \/* Free node *\/<br\/>           now = null;<br\/> <br\/>           \/*Update prev and now *\/<br\/>           prev = prev.next;<br\/>           if (prev != null) <br\/>              now = prev.next;<br\/>       }<br\/>    }                 <br\/> <br\/>                     <br\/>    \/* Utility functions *\/<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\/>    \/* Function to print linked list *\/<br\/>    void printList()<br\/>    {<br\/>        Node temp = head;<br\/>        while(temp != null)<br\/>        {<br\/>           System.out.print(temp.data+&quot; &quot;);<br\/>           temp = temp.next;<br\/>        }  <br\/>        System.out.println();<br\/>    }<br\/> <br\/>     \/* Drier program to test above functions *\/<br\/>    public static void main(String args[])<br\/>    {<br\/>        LinkedList llist = new LinkedList();<br\/>         <br\/>        \/* Constructed Linked List is 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;null *\/<br\/>        llist.push(5);<br\/>        llist.push(4);<br\/>        llist.push(3);<br\/>        llist.push(2);<br\/>        llist.push(1);<br\/>         <br\/>        System.out.println(&quot;Linked List before calling deleteAlt() &quot;);<br\/>        llist.printList();<br\/>         <br\/>        llist.deleteAlt();<br\/>         <br\/>        System.out.println(&quot;Linked List after calling deleteAlt() &quot;);<br\/>        llist.printList();<br\/>    }<br\/>} <br\/>\/* This code is contributed by Rajat Mishra *\/<\/code><\/pre> <\/div>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>List before calling deleteAlt() \r\n1 2 3 4 5 \r\nList after calling deleteAlt() \r\n1 3 5<\/pre>\n<p><span style=\"color: #800000;\"><strong>Time Complexity:<\/strong><\/span> O(n) where n is the number of nodes in the given Linked List.<\/p>\n<h3 id=\"method-2-recursive\"><span style=\"color: #000080;\"><strong>Method 2 (Recursive):<\/strong><\/span><\/h3>\n<p>Recursive code uses the same approach as method 1. The recursive code is simple and short, but causes <strong>O(n)<\/strong> <a href=\"https:\/\/www.wikitechy.com\/technology\/c-algorithm-write-recursive-function-print-reverse-linked-list\/\" target=\"_blank\" rel=\"noopener\">recursive function<\/a> calls for a linked list of size n.<\/p>\n<p><strong><span style=\"color: #333399;\">Java Programming:<\/span><\/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\">\/* deletes alternate nodes of a list starting with head *\/<br\/>void deleteAlt(struct node *head)<br\/>{<br\/>    if (head == NULL)<br\/>        return;<br\/> <br\/>    struct node *node = head-&gt;next;<br\/> <br\/>    if (node == NULL)<br\/>        return;<br\/> <br\/>    \/* Change the next link of head *\/<br\/>    head-&gt;next = node-&gt;next;<br\/> <br\/>    \/* free memory allocated for node *\/<br\/>    free(node);<br\/> <br\/>    \/* Recursively call for the new next of head *\/<br\/>    deleteAlt(head-&gt;next);<br\/>}<\/code><\/pre> <\/div>\n<p><span style=\"color: #800000;\"><strong>Time Complexity:<\/strong><\/span> O(n)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java Algorithm &#8211; Delete alternate nodes of a Linked List &#8211; Linked List &#8211; Given a Singly Linked List, starting from the second node delete all alternate <\/p>\n","protected":false},"author":1,"featured_media":31261,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79476,79478],"tags":[81988,81989,81962,81992,81961,81990,81933,81956,81957,81993,81963,81991,81995,81958,81994,81955,81959],"class_list":["post-27695","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linked-list","category-singly-linked-list","tag-alternating-split-of-a-given-singly-linked-list","tag-delete-alternate-nodes-of-a-circular-linked-list","tag-delete-even-nodes-from-linked-list","tag-delete-even-nodes-from-linked-list-java","tag-delete-odd-nodes-linked-list","tag-delete-odd-nodes-linked-list-java","tag-even-odd-linked-list","tag-given-a-singly-linked-list","tag-group-all-odd-nodes-together-followed-by-the-even-nodes","tag-odd-even-linked-list-python","tag-rearrange-a-linked-list-such-that-all-even-and-odd-positioned-nodes-are-together","tag-remove-odd-numbers-from-linked-list","tag-segregate-even-and-odd-nodes-in-a-linked-list-c","tag-segregate-even-and-odd-nodes-in-a-linked-list-java","tag-segregate-even-and-odd-numbers-in-linked-list","tag-split-a-linked-list-into-even-and-odd-parts","tag-split-linked-list-into-two"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27695","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=27695"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27695\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/31261"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}