{"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-&gt;2-&gt;3-&gt;4\r\nOutput:  1-&gt;3-&gt;2-&gt;4\r\n\r\nInput:   10-&gt;22-&gt;30-&gt;43-&gt;56-&gt;70\r\nOutput:  10-&gt;30-&gt;56-&gt;22-&gt;43-&gt;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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">\/\/ C++ program to rearrange a linked list in such a<br\/>\/\/ way that all odd positioned node are stored before<br\/>\/\/ all even positioned nodes<br\/>#include&lt;bits\/stdc++.h&gt;<br\/>using namespace std;<br\/> <br\/>\/\/ Linked List Node<br\/>struct Node<br\/>{<br\/>    int data;<br\/>    struct Node* next;<br\/>};<br\/> <br\/>\/\/ A utility function to create a new node<br\/>Node* newNode(int key)<br\/>{<br\/>    Node *temp = new Node;<br\/>    temp-&gt;data = key;<br\/>    temp-&gt;next = NULL;<br\/>    return temp;<br\/>}<br\/> <br\/>\/\/ Rearranges given linked list such that all even<br\/>\/\/ positioned nodes are before odd positioned.<br\/>\/\/ Returns new head of linked List.<br\/>Node *rearrangeEvenOdd(Node *head)<br\/>{<br\/>    \/\/ Corner case<br\/>    if (head == NULL)<br\/>        return NULL;<br\/> <br\/>    \/\/ Initialize first nodes of even and<br\/>    \/\/ odd lists<br\/>    Node *odd = head;<br\/>    Node *even = head-&gt;next;<br\/> <br\/>    \/\/ Remember the first node of even list so<br\/>    \/\/ that we can connect the even list at the<br\/>    \/\/ end of odd list.<br\/>    Node *evenFirst = even;<br\/> <br\/>    while (1)<br\/>    {<br\/>        \/\/ If there are no more nodes, then connect<br\/>        \/\/ first node of even list to the last node<br\/>        \/\/ of odd list<br\/>        if (!odd || !even || !(even-&gt;next))<br\/>        {<br\/>            odd-&gt;next = evenFirst;<br\/>            break;<br\/>        }<br\/> <br\/>        \/\/ Connecting odd nodes<br\/>        odd-&gt;next = even-&gt;next;<br\/>        odd = even-&gt;next;<br\/> <br\/>        \/\/ If there are NO more even nodes after<br\/>        \/\/ current odd.<br\/>        if (odd-&gt;next == NULL)<br\/>        {<br\/>            even-&gt;next = NULL;<br\/>            odd-&gt;next = evenFirst;<br\/>            break;<br\/>        }<br\/> <br\/>        \/\/ Connecting even nodes<br\/>        even-&gt;next = odd-&gt;next;<br\/>        even = odd-&gt;next;<br\/>    }<br\/> <br\/>    return head;<br\/>}<br\/> <br\/>\/\/ A utility function to print a linked list<br\/>void printlist(Node * node)<br\/>{<br\/>    while (node != NULL)<br\/>    {<br\/>        cout &lt;&lt; node-&gt;data &lt;&lt; &quot;-&gt;&quot;;<br\/>        node = node-&gt;next;<br\/>    }<br\/>    cout &lt;&lt; &quot;NULL&quot; &lt;&lt; endl;<br\/>}<br\/> <br\/>\/\/ Driver code<br\/>int main(void)<br\/>{<br\/>    Node *head = newNode(1);<br\/>    head-&gt;next = newNode(2);<br\/>    head-&gt;next-&gt;next = newNode(3);<br\/>    head-&gt;next-&gt;next-&gt;next = newNode(4);<br\/>    head-&gt;next-&gt;next-&gt;next-&gt;next = newNode(5);<br\/> <br\/>    cout &lt;&lt; &quot;Given Linked List\\n&quot;;<br\/>    printlist(head);<br\/> <br\/>    head = rearrangeEvenOdd(head);<br\/> <br\/>    cout &lt;&lt; &quot;\\nModified Linked List\\n&quot;;<br\/>    printlist(head);<br\/> <br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>Given Linked List\r\n1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;NULL\r\nModified Linked List\r\n1-&gt;3-&gt;5-&gt;2-&gt;4-&gt;NULL<\/pre>\n<p>&nbsp;<\/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}]}}