{"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->2->3->4->5 then your function should convert it to 1->3->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->2->3->4 then convert it to 1->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[pastacode lang=\u201djava\u201d manual=\u201d%2F%2F%20Java%20program%20to%20delete%20alternate%20nodes%20of%20a%20linked%20list%0Aclass%20LinkedList%0A%7B%0A%20%20%20%20Node%20head%3B%20%20%2F%2F%20head%20of%20list%0A%20%20%0A%20%20%20%20%2F*%20Linked%20list%20Node*%2F%0A%20%20%20%20class%20Node%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20data%3B%0A%20%20%20%20%20%20%20%20Node%20next%3B%0A%20%20%20%20%20%20%20%20Node(int%20d)%20%7Bdata%20%3D%20d%3B%20next%20%3D%20null%3B%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20void%20deleteAlt()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20if%20(head%20%3D%3D%20null)%20%0A%20%20%20%20%20%20%20%20%20%20return%3B%0A%20%0A%20%20%20%20%20%20%20Node%20prev%20%3D%20head%3B%0A%20%20%20%20%20%20%20Node%20now%20%3D%20head.next%3B%0A%20%0A%20%20%20%20%20%20%20while%20(prev%20!%3D%20null%20%26%26%20now%20!%3D%20null)%20%0A%20%20%20%20%20%20%20%7B%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%2F*%20Change%20next%20link%20of%20previus%20node%20*%2F%0A%20%20%20%20%20%20%20%20%20%20%20prev.next%20%3D%20now.next%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%2F*%20Free%20node%20*%2F%0A%20%20%20%20%20%20%20%20%20%20%20now%20%3D%20null%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%2F*Update%20prev%20and%20now%20*%2F%0A%20%20%20%20%20%20%20%20%20%20%20prev%20%3D%20prev.next%3B%0A%20%20%20%20%20%20%20%20%20%20%20if%20(prev%20!%3D%20null)%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20now%20%3D%20prev.next%3B%0A%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%2F*%20Utility%20functions%20*%2F%0A%20%0A%20%20%20%20%2F*%20Inserts%20a%20new%20Node%20at%20front%20of%20the%20list.%20*%2F%0A%20%20%20%20public%20void%20push(int%20new_data)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F*%201%20%26%202%3A%20Allocate%20the%20Node%20%26%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Put%20in%20the%20data*%2F%0A%20%20%20%20%20%20%20%20Node%20new_node%20%3D%20new%20Node(new_data)%3B%0A%20%20%0A%20%20%20%20%20%20%20%20%2F*%203.%20Make%20next%20of%20new%20Node%20as%20head%20*%2F%0A%20%20%20%20%20%20%20%20new_node.next%20%3D%20head%3B%0A%20%20%0A%20%20%20%20%20%20%20%20%2F*%204.%20Move%20the%20head%20to%20point%20to%20new%20Node%20*%2F%0A%20%20%20%20%20%20%20%20head%20%3D%20new_node%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%20Function%20to%20print%20linked%20list%20*%2F%0A%20%20%20%20void%20printList()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20Node%20temp%20%3D%20head%3B%0A%20%20%20%20%20%20%20%20while(temp%20!%3D%20null)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20System.out.print(temp.data%2B%22%20%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20temp%20%3D%20temp.next%3B%0A%20%20%20%20%20%20%20%20%7D%20%20%0A%20%20%20%20%20%20%20%20System.out.println()%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%2F*%20Drier%20program%20to%20test%20above%20functions%20*%2F%0A%20%20%20%20public%20static%20void%20main(String%20args%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20LinkedList%20llist%20%3D%20new%20LinkedList()%3B%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%2F*%20Constructed%20Linked%20List%20is%201-%3E2-%3E3-%3E4-%3E5-%3Enull%20*%2F%0A%20%20%20%20%20%20%20%20llist.push(5)%3B%0A%20%20%20%20%20%20%20%20llist.push(4)%3B%0A%20%20%20%20%20%20%20%20llist.push(3)%3B%0A%20%20%20%20%20%20%20%20llist.push(2)%3B%0A%20%20%20%20%20%20%20%20llist.push(1)%3B%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20System.out.println(%22Linked%20List%20before%20calling%20deleteAlt()%20%22)%3B%0A%20%20%20%20%20%20%20%20llist.printList()%3B%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20llist.deleteAlt()%3B%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20System.out.println(%22Linked%20List%20after%20calling%20deleteAlt()%20%22)%3B%0A%20%20%20%20%20%20%20%20llist.printList()%3B%0A%20%20%20%20%7D%0A%7D%20%0A%2F*%20This%20code%20is%20contributed%20by%20Rajat%20Mishra%20*%2F\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201djava\u201d manual=\u201d%2F*%20deletes%20alternate%20nodes%20of%20a%20list%20starting%20with%20head%20*%2F%0Avoid%20deleteAlt(struct%20node%20*head)%0A%7B%0A%20%20%20%20if%20(head%20%3D%3D%20NULL)%0A%20%20%20%20%20%20%20%20return%3B%0A%20%0A%20%20%20%20struct%20node%20*node%20%3D%20head-%3Enext%3B%0A%20%0A%20%20%20%20if%20(node%20%3D%3D%20NULL)%0A%20%20%20%20%20%20%20%20return%3B%0A%20%0A%20%20%20%20%2F*%20Change%20the%20next%20link%20of%20head%20*%2F%0A%20%20%20%20head-%3Enext%20%3D%20node-%3Enext%3B%0A%20%0A%20%20%20%20%2F*%20free%20memory%20allocated%20for%20node%20*%2F%0A%20%20%20%20free(node)%3B%0A%20%0A%20%20%20%20%2F*%20Recursively%20call%20for%20the%20new%20next%20of%20head%20*%2F%0A%20%20%20%20deleteAlt(head-%3Enext)%3B%0A%7D\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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}]}}