{"id":27670,"date":"2018-04-09T20:20:52","date_gmt":"2018-04-09T14:50:52","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27670"},"modified":"2018-09-14T19:02:38","modified_gmt":"2018-09-14T13:32:38","slug":"rearrange-linked-list-even-odd-positioned-nodes-together","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/rearrange-linked-list-even-odd-positioned-nodes-together\/","title":{"rendered":"Cpp Algorithm-Rearrange a linked list such that all even and odd positioned nodes are together"},"content":{"rendered":"<p>Rearrange a linked list in such a way that all odd position nodes are together and all even positions node are together,<\/p>\n<p><strong>Examples:<\/strong><\/p>\n<pre>Input:   1->2->3->4\r\nOutput:  1->3->2->4\r\n\r\nInput:   10->22->30->43->56->70\r\nOutput:  10->30->56->22->43->70\r\n<\/pre>\n<p>The important thing in this question is to make sure that all below cases are handled<br \/>\n1) Empty linked list<br \/>\n2) A linked list with only one node<br \/>\n3) A linked list with only two nodes<br \/>\n4) A linked list with odd number of nodes<br \/>\n5) A linked list with even number of nodes<\/p>\n<p>The below program maintains two pointers \u2018odd\u2019 and \u2018even\u2019 for current nodes at odd an even positions respectively. We also store first node of even linked list so that we can attach the even list at the end of odd list after all odd and even nodes are connected together in two different lists.<\/p>\n<p><strong>C++ Programming:<\/strong><\/p>\n[pastacode lang=\u201dcpp\u201d manual=\u201d%2F%2F%20C%2B%2B%20program%20to%20rearrange%20a%20linked%20list%20in%20such%20a%0A%2F%2F%20way%20that%20all%20odd%20positioned%20node%20are%20stored%20before%0A%2F%2F%20all%20even%20positioned%20nodes%0A%23include%3Cbits%2Fstdc%2B%2B.h%3E%0Ausing%20namespace%20std%3B%0A%20%0A%2F%2F%20Linked%20List%20Node%0Astruct%20Node%0A%7B%0A%20%20%20%20int%20data%3B%0A%20%20%20%20struct%20Node*%20next%3B%0A%7D%3B%0A%20%0A%2F%2F%20A%20utility%20function%20to%20create%20a%20new%20node%0ANode*%20newNode(int%20key)%0A%7B%0A%20%20%20%20Node%20*temp%20%3D%20new%20Node%3B%0A%20%20%20%20temp-%3Edata%20%3D%20key%3B%0A%20%20%20%20temp-%3Enext%20%3D%20NULL%3B%0A%20%20%20%20return%20temp%3B%0A%7D%0A%20%0A%2F%2F%20Rearranges%20given%20linked%20list%20such%20that%20all%20even%0A%2F%2F%20positioned%20nodes%20are%20before%20odd%20positioned.%0A%2F%2F%20Returns%20new%20head%20of%20linked%20List.%0ANode%20*rearrangeEvenOdd(Node%20*head)%0A%7B%0A%20%20%20%20%2F%2F%20Corner%20case%0A%20%20%20%20if%20(head%20%3D%3D%20NULL)%0A%20%20%20%20%20%20%20%20return%20NULL%3B%0A%20%0A%20%20%20%20%2F%2F%20Initialize%20first%20nodes%20of%20even%20and%0A%20%20%20%20%2F%2F%20odd%20lists%0A%20%20%20%20Node%20*odd%20%3D%20head%3B%0A%20%20%20%20Node%20*even%20%3D%20head-%3Enext%3B%0A%20%0A%20%20%20%20%2F%2F%20Remember%20the%20first%20node%20of%20even%20list%20so%0A%20%20%20%20%2F%2F%20that%20we%20can%20connect%20the%20even%20list%20at%20the%0A%20%20%20%20%2F%2F%20end%20of%20odd%20list.%0A%20%20%20%20Node%20*evenFirst%20%3D%20even%3B%0A%20%0A%20%20%20%20while%20(1)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20there%20are%20no%20more%20nodes%2C%20then%20connect%0A%20%20%20%20%20%20%20%20%2F%2F%20first%20node%20of%20even%20list%20to%20the%20last%20node%0A%20%20%20%20%20%20%20%20%2F%2F%20of%20odd%20list%0A%20%20%20%20%20%20%20%20if%20(!odd%20%7C%7C%20!even%20%7C%7C%20!(even-%3Enext))%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20odd-%3Enext%20%3D%20evenFirst%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Connecting%20odd%20nodes%0A%20%20%20%20%20%20%20%20odd-%3Enext%20%3D%20even-%3Enext%3B%0A%20%20%20%20%20%20%20%20odd%20%3D%20even-%3Enext%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20there%20are%20NO%20more%20even%20nodes%20after%0A%20%20%20%20%20%20%20%20%2F%2F%20current%20odd.%0A%20%20%20%20%20%20%20%20if%20(odd-%3Enext%20%3D%3D%20NULL)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20even-%3Enext%20%3D%20NULL%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20odd-%3Enext%20%3D%20evenFirst%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Connecting%20even%20nodes%0A%20%20%20%20%20%20%20%20even-%3Enext%20%3D%20odd-%3Enext%3B%0A%20%20%20%20%20%20%20%20even%20%3D%20odd-%3Enext%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20return%20head%3B%0A%7D%0A%20%0A%2F%2F%20A%20utility%20function%20to%20print%20a%20linked%20list%0Avoid%20printlist(Node%20*%20node)%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%20cout%20%3C%3C%20node-%3Edata%20%3C%3C%20%22-%3E%22%3B%0A%20%20%20%20%20%20%20%20node%20%3D%20node-%3Enext%3B%0A%20%20%20%20%7D%0A%20%20%20%20cout%20%3C%3C%20%22NULL%22%20%3C%3C%20endl%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20code%0Aint%20main(void)%0A%7B%0A%20%20%20%20Node%20*head%20%3D%20newNode(1)%3B%0A%20%20%20%20head-%3Enext%20%3D%20newNode(2)%3B%0A%20%20%20%20head-%3Enext-%3Enext%20%3D%20newNode(3)%3B%0A%20%20%20%20head-%3Enext-%3Enext-%3Enext%20%3D%20newNode(4)%3B%0A%20%20%20%20head-%3Enext-%3Enext-%3Enext-%3Enext%20%3D%20newNode(5)%3B%0A%20%0A%20%20%20%20cout%20%3C%3C%20%22Given%20Linked%20List%5Cn%22%3B%0A%20%20%20%20printlist(head)%3B%0A%20%0A%20%20%20%20head%20%3D%20rearrangeEvenOdd(head)%3B%0A%20%0A%20%20%20%20cout%20%3C%3C%20%22%5CnModified%20Linked%20List%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>Given Linked List\r\n1->2->3->4->5->NULL\r\nModified Linked List\r\n1->3->5->2->4->NULL<\/pre>\n<p>\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Rearrange a linked list such that all even and odd positioned nodes are together-linked list<br \/>\nRearrange a linked list in such a way that all odd position <\/p>\n","protected":false},"author":1,"featured_media":31274,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79476,79478],"tags":[81962,81961,81933,81956,81957,81954,81960,81963,81958,81955,81959],"class_list":["post-27670","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linked-list","category-singly-linked-list","tag-delete-even-nodes-from-linked-list","tag-delete-odd-nodes-linked-list","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-java","tag-odd-even-linked-list-leetcode-c","tag-rearrange-a-linked-list-such-that-all-even-and-odd-positioned-nodes-are-together","tag-segregate-even-and-odd-nodes-in-a-linked-list-java","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\/27670","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=27670"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27670\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/31274"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27670"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27670"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27670"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}