{"id":27590,"date":"2018-04-04T19:32:56","date_gmt":"2018-04-04T14:02:56","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27590"},"modified":"2018-04-04T19:32:56","modified_gmt":"2018-04-04T14:02:56","slug":"c-algorithm-pairwise-swap-elements-given-linked-list","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-algorithm-pairwise-swap-elements-given-linked-list\/","title":{"rendered":"C Algorithm &#8211; Pairwise swap elements of a given linked list"},"content":{"rendered":"<p>Given a singly linked list, write a function to swap elements pairwise. For example, if the linked list is 1-&gt;2-&gt;3-&gt;4-&gt;5 then the function should change it to 2-&gt;1-&gt;4-&gt;3-&gt;5, and if the linked list is 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;6 then the function should change it to 2-&gt;1-&gt;4-&gt;3-&gt;6-&gt;5.<span id=\"more-7049\"><\/span><\/p>\n<div id=\"practice\">\u00a0<strong>METHOD 1 (Iterative) <\/strong><br \/>\nStart from the head node and traverse the list. While traversing swap data of each node with its next node\u2019s data<\/div>\n<div><\/div>\n<div><strong>C Programming:<\/strong><\/div>\n<div>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/* C program to pairwise swap elements in a given linked list *\/<br\/>#include&lt;stdio.h&gt;<br\/>#include&lt;stdlib.h&gt;<br\/> <br\/>\/* A linked list node *\/<br\/>struct node<br\/>{<br\/>    int data;<br\/>    struct node *next;<br\/>};<br\/> <br\/>\/*Function to swap two integers at addresses a and b *\/<br\/>void swap(int *a, int *b);<br\/> <br\/>\/* Function to pairwise swap elements of a linked list *\/<br\/>void pairWiseSwap(struct node *head)<br\/>{<br\/>    struct node *temp = head;<br\/> <br\/>    \/* Traverse further only if there are at-least two nodes left *\/<br\/>    while (temp != NULL &amp;&amp; temp-&gt;next != NULL)<br\/>    {<br\/>        \/* Swap data of node with its next node&#039;s data *\/<br\/>        swap(&amp;temp-&gt;data, &amp;temp-&gt;next-&gt;data);<br\/> <br\/>        \/* Move temp by 2 for the next pair *\/<br\/>        temp = temp-&gt;next-&gt;next;<br\/>    }<br\/>}<br\/> <br\/>\/* UTILITY FUNCTIONS *\/<br\/>\/* Function to swap two integers *\/<br\/>void swap(int *a, int *b)<br\/>{<br\/>    int temp;<br\/>    temp = *a;<br\/>    *a = *b;<br\/>    *b = temp;<br\/>}<br\/> <br\/>\/* Function to add a node at the begining of Linked List *\/<br\/>void push(struct node** head_ref, int new_data)<br\/>{<br\/>    \/* allocate node *\/<br\/>    struct node* new_node =<br\/>              (struct node*) malloc(sizeof(struct 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 nodes in a given linked list *\/<br\/>void printList(struct node *node)<br\/>{<br\/>    while (node != NULL)<br\/>    {<br\/>        printf(&quot;%d &quot;, node-&gt;data);<br\/>       node = node-&gt;next;<br\/>    }<br\/>}<br\/> <br\/>\/* Driver program to test above function *\/<br\/>int main()<br\/>{<br\/>    struct node *start = NULL;<br\/> <br\/>    \/* The constructed linked list is:<br\/>     1-&gt;2-&gt;3-&gt;4-&gt;5 *\/<br\/>    push(&amp;start, 5);<br\/>    push(&amp;start, 4);<br\/>    push(&amp;start, 3);<br\/>    push(&amp;start, 2);<br\/>    push(&amp;start, 1);<br\/> <br\/>    printf(&quot;Linked list before calling  pairWiseSwap()\\n&quot;);<br\/>    printList(start);<br\/> <br\/>    pairWiseSwap(start);<br\/> <br\/>    printf(&quot;\\nLinked list after calling  pairWiseSwap()\\n&quot;);<br\/>    printList(start);<br\/> <br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>Linked List before calling pairWiseSwap() \r\n1 2 3 4 5 \r\nLinked List after calling pairWiseSwap() \r\n2 1 4 3 5<\/pre>\n<p>Time complexity: O(n)<\/p>\n<p><strong>METHOD 2 (Recursive)<\/strong><br \/>\nIf there are 2 or more than 2 nodes in Linked List then swap the first two nodes and recursively call for rest of the list.<\/p>\n<div>\u00a0<strong>C Programming:<\/strong><\/div>\n<div>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/* Recursive function to pairwise swap elements of a linked list *\/<br\/>void pairWiseSwap(struct node *head)<br\/>{<br\/>  \/* There must be at-least two nodes in the list *\/<br\/>  if (head != NULL &amp;&amp; head-&gt;next != NULL)<br\/>  {<br\/>      \/* Swap the node&#039;s data with data of next node *\/<br\/>      swap(&amp;head-&gt;data, &amp;head-&gt;next-&gt;data);<br\/>    <br\/>      \/* Call pairWiseSwap() for rest of the list *\/<br\/>      pairWiseSwap(head-&gt;next-&gt;next);<br\/>  }  <br\/>}<\/code><\/pre> <\/div>\n<\/div>\n<div>Time complexity: O(n)<\/div>\n<div><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>C Algorithm &#8211; Pairwise swap elements of a given linked list &#8211; Linked List &#8211; Given a singly linked list, write a function to swap elements pairwise<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79476,79478],"tags":[80425,81794,80431,80424,81791,81792,80430,81793,80420,80421],"class_list":["post-27590","post","type-post","status-publish","format-standard","hentry","category-linked-list","category-singly-linked-list","tag-how-to-swap-two-nodes-in-a-doubly-linked-list-java","tag-reverse-linked-list-in-pairs-java","tag-swap-adjacent-nodes-in-linked-list-java","tag-swap-alternate-nodes-linked-list","tag-swap-list-nodes-in-pairs","tag-swap-nodes-doubly-linked-list-c","tag-swap-nodes-in-linked-list","tag-swap-nodes-in-pairs-java","tag-swap-two-nodes-in-a-linked-list-java","tag-write-a-program-to-reverse-a-linked-list"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27590","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=27590"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27590\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27590"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27590"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27590"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}