{"id":27648,"date":"2018-04-09T20:10:11","date_gmt":"2018-04-09T14:40:11","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27648"},"modified":"2018-09-16T13:31:31","modified_gmt":"2018-09-16T08:01:31","slug":"cpp-algorithm-rearrange-a-linked-list-in-zig-zag-fashion","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/cpp-algorithm-rearrange-a-linked-list-in-zig-zag-fashion\/","title":{"rendered":"Cpp Algorithm-Rearrange a Linked List in Zig-Zag fashion"},"content":{"rendered":"<p>Given a linked list, rearrange it such that converted list should be of the form a &lt; b &gt; c &lt; d &gt; e &lt; f .. where a, b, c.. are consecutive data node of linked list. Examples :<\/p>\n<pre>Input:  1-&gt;2-&gt;3-&gt;4\r\nOutput: 1-&gt;3-&gt;2-&gt;4 \r\n\r\nInput:  11-&gt;15-&gt;20-&gt;5-&gt;10\r\nOutput: 11-&gt;20-&gt;5-&gt;15-&gt;10\r\n\r\n<\/pre>\n<p>A <strong>simple approach<\/strong> to do this, is to <a href=\"http:\/\/www.geeksforgeeks.org\/merge-sort-for-linked-list\/\" target=\"_blank\" rel=\"noopener\">sort the linked list using merge sort<\/a> and then swap alternate, but that requires O(n Log n) time complexity. Here n is number of elements in linked list.<\/p>\n<p>An <strong>efficient approach<\/strong> which requires O(n) time is, using a single scan similar to bubble sort and then maintain a flag for representing which order (&lt; or &gt;) currently we are. If the current two elements are not in that order then swap those elements otherwise not. Please refer <a href=\"http:\/\/geeksquiz.com\/converting-an-array-of-integers-into-zig-zag-fashion\/\" target=\"_blank\" rel=\"noopener\">this<\/a> for detailed explanation of swapping order.<\/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 arrange linked list in zigzag fashion<br\/>#include &lt;bits\/stdc++.h&gt;<br\/>using namespace std;<br\/> <br\/>\/* Link list Node *\/<br\/>struct Node<br\/>{<br\/>    int data;<br\/>    struct Node* next;<br\/>};<br\/> <br\/>\/\/ This function distributes the Node in zigzag fashion<br\/>void zigZagList(Node *head)<br\/>{<br\/>    \/\/ If flag is true, then next node should be greater<br\/>    \/\/ in the desired output.<br\/>    bool flag = true;<br\/> <br\/>    \/\/ Traverse linked list starting from head.<br\/>    Node* current = head;<br\/>    while (current-&gt;next != NULL)<br\/>    {<br\/>        if (flag)  \/* &quot;&lt;&quot; relation expected *\/<br\/>        {<br\/>            \/* If we have a situation like A &gt; B &gt; C<br\/>               where A, B and C are consecutive Nodes<br\/>               in list we get A &gt; B &lt; C by swapping B<br\/>               and C *\/<br\/>            if (current-&gt;data &gt; current-&gt;next-&gt;data)<br\/>                swap(current-&gt;data, current-&gt;next-&gt;data);<br\/>        }<br\/>        else \/* &quot;&gt;&quot; relation expected *\/<br\/>        {<br\/>            \/* If we have a situation like A &lt; B &lt; C where<br\/>               A, B and C  are consecutive Nodes in list we<br\/>               get A &lt; C &gt; B by swapping B and C *\/<br\/>            if (current-&gt;data &lt; current-&gt;next-&gt;data)<br\/>                swap(current-&gt;data, current-&gt;next-&gt;data);<br\/>        }<br\/> <br\/>        current = current-&gt;next;<br\/>        flag = !flag;  \/* flip flag for reverse checking *\/<br\/>    }<br\/>}<br\/> <br\/>\/* UTILITY FUNCTIONS *\/<br\/>\/* Function to push a Node *\/<br\/>void push(Node** head_ref, int new_data)<br\/>{<br\/>    \/* allocate Node *\/<br\/>    struct Node* new_Node = new Node;<br\/> <br\/>    \/* put in the data  *\/<br\/>    new_Node-&gt;data  = new_data;<br\/> <br\/>    \/* link the old list off the new Node *\/<br\/>    new_Node-&gt;next = (*head_ref);<br\/> <br\/>    \/* move the head to point to the new Node *\/<br\/>    (*head_ref)    = new_Node;<br\/>}<br\/> <br\/>\/* Function to print linked list *\/<br\/>void printList(struct Node *Node)<br\/>{<br\/>    while (Node != NULL)<br\/>    {<br\/>        printf(&quot;%d-&gt;&quot;, Node-&gt;data);<br\/>        Node = Node-&gt;next;<br\/>    }<br\/>    printf(&quot;NULL&quot;);<br\/>}<br\/> <br\/>\/* Drier program to test above function*\/<br\/>int main(void)<br\/>{<br\/>    \/* Start with the empty list *\/<br\/>    struct Node* head = NULL;<br\/> <br\/>    \/\/ create a list 4 -&gt; 3 -&gt; 7 -&gt; 8 -&gt; 6 -&gt; 2 -&gt; 1<br\/>    \/\/ answer should be -&gt; 3  7  4  8  2  6  1<br\/>    push(&amp;head, 1);<br\/>    push(&amp;head, 2);<br\/>    push(&amp;head, 6);<br\/>    push(&amp;head, 8);<br\/>    push(&amp;head, 7);<br\/>    push(&amp;head, 3);<br\/>    push(&amp;head, 4);<br\/> <br\/>    printf(&quot;Given linked list \\n&quot;);<br\/>    printList(head);<br\/> <br\/>    zigZagList(head);<br\/> <br\/>    printf(&quot;\\nZig Zag 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\n4-&gt;3-&gt;7-&gt;8-&gt;6-&gt;2-&gt;1-&gt;NULL\r\n\r\nZig Zag Linked list \r\n3-&gt;7-&gt;4-&gt;8-&gt;2-&gt;6-&gt;1-&gt;NULL<\/pre>\n<p>In above code, push function pushes the node at the front of the linked list, the code can be easily modified for pushing node at the end of list. Other thing to note is, swapping of data between two nodes is done by swap by value not swap by links for simplicity, for swap by links technique please see <a href=\"http:\/\/www.geeksforgeeks.org\/swap-nodes-in-a-linked-list-without-swapping-data\/\" target=\"_blank\" rel=\"noopener\">this<\/a>.<\/p>\n<p>Time complexity : O(n)<br \/>\nAuxiliary Space : O(1)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cpp Algorithm-Rearrange a Linked List in Zig-Zag fashion-Linked list<br \/>\nGiven a linked list, rearrange it such that converted list should be <\/p>\n","protected":false},"author":1,"featured_media":31287,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79476,79478],"tags":[81795,81927,81933,81926,81934,81935,81936,81929,81931,81930,81932,81928],"class_list":["post-27648","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linked-list","category-singly-linked-list","tag-add-1-to-a-number-represented-as-linked-list","tag-delete-nodes-having-greater-value-on-right","tag-even-odd-linked-list","tag-rearrange-linkedin-list","tag-reorder-linked-list","tag-reorder-linkedin","tag-reorder-list","tag-tortoise-and-hare-method","tag-zigzag-array-algorithm","tag-zigzag-array-hackerrank-solution-java","tag-zigzag-array-in-java","tag-zigzag-matrix-algorithm"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27648","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=27648"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27648\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/31287"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27648"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27648"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27648"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}