{"id":27597,"date":"2018-04-04T19:33:21","date_gmt":"2018-04-04T14:03:21","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27597"},"modified":"2018-04-04T19:33:21","modified_gmt":"2018-04-04T14:03:21","slug":"python-algorithm-pairwise-swap-elements-given-linked-list","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/python-algorithm-pairwise-swap-elements-given-linked-list\/","title":{"rendered":"Python 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>Python Programming:<\/strong><\/div>\n<div>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\"># Python program to swap the elements of linked list pairwise<br\/> <br\/># Node class <br\/>class Node:<br\/> <br\/>    # Constructor to initialize the node object<br\/>    def __init__(self, data):<br\/>        self.data = data<br\/>        self.next = None<br\/> <br\/>class LinkedList:<br\/> <br\/>    # Function to initialize head<br\/>    def __init__(self):<br\/>        self.head = None<br\/> <br\/>    # Function to pairwise swap elements of a linked list<br\/>    def pairwiseSwap(self):<br\/>        temp = self.head<br\/>        <br\/>        # There are no nodes in ilnked list<br\/>        if temp is None:<br\/>            return<br\/>          <br\/>        # Traverse furthur only if there are at least two<br\/>        #  left<br\/>        while(temp is not None and temp.next is not None):<br\/> <br\/>            # Swap data of node with its next node&#039;s data<br\/>            temp.data, temp.next.data = temp.next.data, temp.data <br\/>             <br\/>            # Move temo by 2 fro the next pair<br\/>            temp = temp.next.next<br\/>        <br\/>    # Function to insert a new node at the beginning<br\/>    def push(self, new_data):<br\/>        new_node = Node(new_data)<br\/>        new_node.next = self.head<br\/>        self.head = new_node<br\/> <br\/>    # Utility function to prit the linked LinkedList<br\/>    def printList(self):<br\/>        temp = self.head<br\/>        while(temp):<br\/>            print temp.data,<br\/>            temp = temp.next<br\/> <br\/> <br\/># Driver program<br\/>llist = LinkedList()<br\/>llist.push(5)<br\/>llist.push(4)<br\/>llist.push(3)<br\/>llist.push(2)<br\/>llist.push(1)<br\/> <br\/>print &quot;Linked list before calling pairWiseSwap() &quot;<br\/>llist.printList()<br\/> <br\/>llist.pairwiseSwap()<br\/> <br\/>print  &quot;\\nLinked list after calling pairWiseSwap()&quot;<br\/>llist.printList()<br\/> <br\/># This code is contributed by Nikhil Kumar Singh(nickzuck_007)<\/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>Python\u00a0Programming:<\/strong><\/div>\n<div>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python 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>Python 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-27597","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\/27597","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=27597"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27597\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27597"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27597"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27597"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}