{"id":27694,"date":"2018-04-09T20:22:47","date_gmt":"2018-04-09T14:52:47","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27694"},"modified":"2018-09-11T19:35:24","modified_gmt":"2018-09-11T14:05:24","slug":"cpp-algorithm-delete-alternate-nodes-linked-list","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/cpp-algorithm-delete-alternate-nodes-linked-list\/","title":{"rendered":"Cpp Algorithm &#8211; Delete alternate nodes of a Linked List"},"content":{"rendered":"<p>Given a Singly Linked List, starting from the second node delete all alternate nodes of it. For example, if the given linked list is 1->2->3->4->5 then your function should convert it to 1->3->5, and if the given linked list is 1->2->3->4 then convert it to 1->3.<span id=\"more-7443\"><\/span><\/p>\n<p><strong>Method 1 (Iterative)<\/strong><br \/>\nKeep 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<p><strong>C++ Programming:<\/strong><\/p>\n[pastacode lang=\u201dcpp\u201d manual=\u201d%2F%2F%20C%20program%20to%20remove%20alternate%20nodes%20of%20a%20linked%20list%0A%23include%3Cstdio.h%3E%0A%23include%3Cstdlib.h%3E%0A%20%0A%2F*%20A%20linked%20list%20node%20*%2F%0Astruct%20node%0A%7B%0A%20%20%20%20int%20data%3B%0A%20%20%20%20struct%20node%20*next%3B%0A%7D%3B%0A%20%0A%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%20%2F*%20Initialize%20prev%20and%20node%20to%20be%20deleted%20*%2F%0A%20%20%20%20struct%20node%20*prev%20%3D%20head%3B%0A%20%20%20%20struct%20node%20*node%20%3D%20head-%3Enext%3B%0A%20%0A%20%20%20%20while%20(prev%20!%3D%20NULL%20%26%26%20node%20!%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F*%20Change%20next%20link%20of%20previous%20node%20*%2F%0A%20%20%20%20%20%20%20%20prev-%3Enext%20%3D%20node-%3Enext%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20Free%20memory%20*%2F%0A%20%20%20%20%20%20%20%20free(node)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20Update%20prev%20and%20node%20*%2F%0A%20%20%20%20%20%20%20%20prev%20%3D%20prev-%3Enext%3B%0A%20%20%20%20%20%20%20%20if%20(prev%20!%3D%20NULL)%0A%20%20%20%20%20%20%20%20%20%20%20%20node%20%3D%20prev-%3Enext%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F*%20UTILITY%20FUNCTIONS%20TO%20TEST%20fun1()%20and%20fun2()%20*%2F%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(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%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%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*%20Function%20to%20print%20nodes%20in%20a%20given%20linked%20list%20*%2F%0Avoid%20printList(struct%20node%20*node)%0A%7B%0A%20%20%20%20while%20(node%20!%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20printf(%22%25d%20%22%2C%20node-%3Edata)%3B%0A%20%20%20%20%20%20%20%20node%20%3D%20node-%3Enext%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F*%20Drier%20program%20to%20test%20above%20functions%20*%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%0A%20%20%20%20%2F*%20Using%20push()%20to%20construct%20below%20list%0A%20%20%20%20%20%201-%3E2-%3E3-%3E4-%3E5%20%20*%2F%0A%20%20%20%20push(%26head%2C%205)%3B%0A%20%20%20%20push(%26head%2C%204)%3B%0A%20%20%20%20push(%26head%2C%203)%3B%0A%20%20%20%20push(%26head%2C%202)%3B%0A%20%20%20%20push(%26head%2C%201)%3B%0A%20%0A%20%20%20%20printf(%22%5CnList%20before%20calling%20deleteAlt()%20%5Cn%22)%3B%0A%20%20%20%20printList(head)%3B%0A%20%0A%20%20%20%20deleteAlt(head)%3B%0A%20%0A%20%20%20%20printf(%22%5CnList%20after%20calling%20deleteAlt()%20%5Cn%22)%3B%0A%20%20%20%20printList(head)%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D%0A\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Output:<\/strong><\/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>Time Complexity: O(n) where n is the number of nodes in the given Linked List.<\/p>\n<p><strong>Method 2 (Recursive)<\/strong><br \/>\nRecursive code uses the same approach as method 1. The recursive coed is simple and short, but causes O(n) recursive function calls for a linked list of size n.<\/p>\n<p><strong>C++ Programming:<\/strong><\/p>\n[pastacode lang=\u201dcpp\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>Time Complexity: O(n)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cpp 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":31263,"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-27694","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\/27694","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=27694"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27694\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/31263"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27694"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27694"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27694"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}