{"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->2->3->4->5 then the function should change it to 2->1->4->3->5, and if the linked list is 1->2->3->4->5->6 then the function should change it to 2->1->4->3->6->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[pastacode lang=\u201dpython\u201d manual=\u201d%23%20Python%20program%20to%20swap%20the%20elements%20of%20linked%20list%20pairwise%0A%20%0A%23%20Node%20class%20%0Aclass%20Node%3A%0A%20%0A%20%20%20%20%23%20Constructor%20to%20initialize%20the%20node%20object%0A%20%20%20%20def%20__init__(self%2C%20data)%3A%0A%20%20%20%20%20%20%20%20self.data%20%3D%20data%0A%20%20%20%20%20%20%20%20self.next%20%3D%20None%0A%20%0Aclass%20LinkedList%3A%0A%20%0A%20%20%20%20%23%20Function%20to%20initialize%20head%0A%20%20%20%20def%20__init__(self)%3A%0A%20%20%20%20%20%20%20%20self.head%20%3D%20None%0A%20%0A%20%20%20%20%23%20Function%20to%20pairwise%20swap%20elements%20of%20a%20linked%20list%0A%20%20%20%20def%20pairwiseSwap(self)%3A%0A%20%20%20%20%20%20%20%20temp%20%3D%20self.head%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%23%20There%20are%20no%20nodes%20in%20ilnked%20list%0A%20%20%20%20%20%20%20%20if%20temp%20is%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%0A%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%23%20Traverse%20furthur%20only%20if%20there%20are%20at%20least%20two%0A%20%20%20%20%20%20%20%20%23%20%20left%0A%20%20%20%20%20%20%20%20while(temp%20is%20not%20None%20and%20temp.next%20is%20not%20None)%3A%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Swap%20data%20of%20node%20with%20its%20next%20node\u2019s%20data%0A%20%20%20%20%20%20%20%20%20%20%20%20temp.data%2C%20temp.next.data%20%3D%20temp.next.data%2C%20temp.data%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Move%20temo%20by%202%20fro%20the%20next%20pair%0A%20%20%20%20%20%20%20%20%20%20%20%20temp%20%3D%20temp.next.next%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%23%20Function%20to%20insert%20a%20new%20node%20at%20the%20beginning%0A%20%20%20%20def%20push(self%2C%20new_data)%3A%0A%20%20%20%20%20%20%20%20new_node%20%3D%20Node(new_data)%0A%20%20%20%20%20%20%20%20new_node.next%20%3D%20self.head%0A%20%20%20%20%20%20%20%20self.head%20%3D%20new_node%0A%20%0A%20%20%20%20%23%20Utility%20function%20to%20prit%20the%20linked%20LinkedList%0A%20%20%20%20def%20printList(self)%3A%0A%20%20%20%20%20%20%20%20temp%20%3D%20self.head%0A%20%20%20%20%20%20%20%20while(temp)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print%20temp.data%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20temp%20%3D%20temp.next%0A%20%0A%20%0A%23%20Driver%20program%0Allist%20%3D%20LinkedList()%0Allist.push(5)%0Allist.push(4)%0Allist.push(3)%0Allist.push(2)%0Allist.push(1)%0A%20%0Aprint%20%22Linked%20list%20before%20calling%20pairWiseSwap()%20%22%0Allist.printList()%0A%20%0Allist.pairwiseSwap()%0A%20%0Aprint%20%20%22%5CnLinked%20list%20after%20calling%20pairWiseSwap()%22%0Allist.printList()%0A%20%0A%23%20This%20code%20is%20contributed%20by%20Nikhil%20Kumar%20Singh(nickzuck_007)\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201dpython\u201d manual=\u201d%2F*%20Recursive%20function%20to%20pairwise%20swap%20elements%20of%20a%20linked%20list%20*%2F%0Avoid%20pairWiseSwap(struct%20node%20*head)%0A%7B%0A%20%20%2F*%20There%20must%20be%20at-least%20two%20nodes%20in%20the%20list%20*%2F%0A%20%20if%20(head%20!%3D%20NULL%20%26%26%20head-%3Enext%20!%3D%20NULL)%0A%20%20%7B%0A%20%20%20%20%20%20%2F*%20Swap%20the%20node\u2019s%20data%20with%20data%20of%20next%20node%20*%2F%0A%20%20%20%20%20%20swap(%26head-%3Edata%2C%20%26head-%3Enext-%3Edata)%3B%0A%20%20%20%20%0A%20%20%20%20%20%20%2F*%20Call%20pairWiseSwap()%20for%20rest%20of%20the%20list%20*%2F%0A%20%20%20%20%20%20pairWiseSwap(head-%3Enext-%3Enext)%3B%0A%20%20%7D%20%20%0A%7D\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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}]}}