{"id":3461,"date":"2021-08-03T08:59:16","date_gmt":"2021-08-03T08:59:16","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=3461"},"modified":"2022-11-18T07:21:07","modified_gmt":"2022-11-18T07:21:07","slug":"the-most-efficient-way-to-pairwise-swap-elements-of-a-given-linked-list","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/the-most-efficient-way-to-pairwise-swap-elements-of-a-given-linked-list\/","title":{"rendered":"The most efficient way to Pairwise swap elements of a given linked list"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644921996084-86%20Pairwise-05.png\" alt=\"\" \/><\/p>\n<p>In this article, we will understand how to pairwise swap elements of a linked list.<br \/>\nLinked list as a sequence of data structures which are connected through links and each node is having a pointer which points to the next node. let\u2019s try how we can pairwise swap elements of a linked list. <\/p>\n<h3>Problem Statement<\/h3>\n<p>In this problem, we are given a linked list and we have to swap the elements in a pairwise manner. It is not allowed to swap the data, only links should be changed.<\/p>\n<\/p>\n<p>Let me explain pairwise swap of a linked list with an example &#8211; if the linked list is<br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/52_1-01.png\" alt=\"\" \/><br \/>\nthen the function should change it to<br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/52_2-01.png\" alt=\"\" \/><br \/>\nWell, this is one of the most popular questions to be asked in interviews and may seem a bit difficult at first but it is easy to comprehend.<\/p>\n<h3>Problem Statement Understanding to pairwise swap elements of a linked list<\/h3>\n<p>Let&#8217;s first understand the problem statement with the help of an example.<br \/>\nLinked list = 1-&gt;2-&gt;3-&gt;4-&gt;5,<br \/>\nThe term <strong>Pairwise swap<\/strong> indicates that we need to swap the positions of the adjacent two nodes in the linked list.<br \/>\nSo,<\/p>\n<ul>\n<li>1-&gt;2 will become 2-&gt;1<\/li>\n<li>3-&gt;4 will become 4-&gt;3<\/li>\n<li>5 has no one to be paired with hence it remains as it is.<\/li>\n<\/ul>\n<p>Finally the linked list will be = 2-&gt;1-&gt;4-&gt;3-&gt;5.<\/p>\n<p>Let&#8217;s take an example with an even-length linked list.<br \/>\nLinked list = 4-&gt;1-&gt;6-&gt;3-&gt;8-&gt;9<br \/>\nSo performing the pairwise swap,<\/p>\n<ul>\n<li>4-&gt;1 will become 1-&gt;4.<\/li>\n<li>6-&gt;3 will become 3-&gt;6.<\/li>\n<li>8-&gt;9 will become 9-8.<\/li>\n<\/ul>\n<p>Finally the linked list will be = 1-&gt;4-&gt;3-&gt;6-&gt;9-&gt;8.<\/p>\n<p>You should take more examples and get the output according to the above understanding to pairwise swap elements of a linked list. Analyzing different examples will help you create the logic for this question.<\/p>\n<h3>Approach to pairwise swap of linked list<\/h3>\n<p>I hope you got a basic idea on what we need to do to solve this question.<br \/>\nThe idea is simple, we have to change the links of the nodes alternatively for every 2 nodes. We will traverse the linked list from the beginning and for every two nodes, we will change the pointers of the next nodes to the previous nodes.<\/p>\n<p>Since it is clear <strong>what<\/strong> we need to do to pairwise swap elements of a linked list, take some time and think on <strong>how<\/strong> we are going to do it.<\/p>\n<h4>Helpful Observations<\/h4>\n<ol>\n<li>\n<p>Since we need to swap two nodes, that is change the links, we should have two pointers pointing on the nodes.<\/p>\n<\/li>\n<li>\n<p>Suppose linked list = &amp;1-&gt;2-&gt;3-&gt;4,and two pointers be prev and curr.<\/p>\n<ol>\n<li>The prev pointing to the node(1) and curr pointing to the node(2).<\/li>\n<li>We can see that we need to change curr-&gt;next and point it to prev i.e 2-&gt;1.<\/li>\n<li>Also finally the linked list will be 2-&gt;1-&gt;4-&gt;3 i.e. the next of nodes with value 1 will point to the node with value 4, so that means prev-&gt;next should be curr-&gt;next-&gt;next. (Think why this step should be done before step 2).<\/li>\n<li>Step 3 should be done before Step 2 because we need to access node(4) from node(2) but in step 2, node(2)-&gt;next is changed to node(1), hence connection to node(4) is lost.<\/li>\n<li>Or we can simply store in temporary node node(2)-&gt;next and follow the above steps in given order.<\/li>\n<\/ol>\n<\/li>\n<li>\n<p>There are some corner conditions to handle, take some examples, use the above observations and try to get those corner conditions.<\/p>\n<\/li>\n<li>\n<p>Below is the proper algorithm to pairwise swap elements of a linked list.<\/p>\n<\/li>\n<\/ol>\n<h3>Algorithm for a pairwise swap of a linked list<\/h3>\n<ul>\n<li>Initialize prev and curr pointers.<\/li>\n<li>Traverse the list, store in the temp node the value of curr-&gt; next and change the next of curr as of the prev node.<\/li>\n<li>If temp is NULL or temp is the last node then change prev-&gt; next to NULL and break the iteration. (Above mentioned corner conditions).<\/li>\n<li>Else we have to change next of prev to next of curr.<\/li>\n<li>Update prev and curr nodes for the next iteration.<br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/52_3-01.png\" alt=\"\" \/><\/li>\n<\/ul>\n<p><strong>Code Implementation for a pairwise swap of a linked list<\/strong><br \/>\n\t\t\t\t\t\t\t<h3 style=\"margin-bottom:20px ;display:block;width:100%;margin-top:10px\">The most efficient way to Pairwise swap elements of a given linked list <\/h3>\r\n\t\t\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_3463 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_3463 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_3463 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_3463 .wpsm_nav-tabs > li.active > a, #tab_container_3463 .wpsm_nav-tabs > li.active > a:hover, #tab_container_3463 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_3463 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_3463 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_3463 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_3463 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_3463 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_3463 .wpsm_nav-tabs > li > a:hover , #tab_container_3463 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_3463 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_3463 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_3463 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_3463 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_3463 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_3463 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_3463 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_3463 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_3463 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_3463 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_3463 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_3463\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_3463\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_3463_1\" aria-controls=\"tabs_desc_3463_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_3463_2\" aria-controls=\"tabs_desc_3463_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_3463_3\" aria-controls=\"tabs_desc_3463_3\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_3463_4\" aria-controls=\"tabs_desc_3463_4\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Python<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_3463\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_3463_1\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"c\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n \r\n\/* A linked list node *\/\r\nstruct Node {\r\n    int data;\r\n    struct Node* next;\r\n};\r\n \r\n\/*Function to swap two integers at addresses a and b *\/\r\nvoid swap(int* a, int* b);\r\n \r\n\/* Function to pairwise swap elements of a linked list *\/\r\nvoid pairWiseSwap(struct Node* head)\r\n{\r\n    struct Node* temp = head;\r\n \r\n    \/* Traverse further only if there are at-least two nodes left *\/\r\n    while (temp != NULL &amp;&amp; temp-&gt;next != NULL) {\r\n        \/* Swap data of node with its next node's data *\/\r\n        swap(&amp;temp-&gt;data, &amp;temp-&gt;next-&gt;data);\r\n \r\n        \/* Move temp by 2 for the next pair *\/\r\n        temp = temp-&gt;next-&gt;next;\r\n    }\r\n}\r\n \r\n\/* UTILITY FUNCTIONS *\/\r\n\/* Function to swap two integers *\/\r\nvoid swap(int* a, int* b)\r\n{\r\n    int temp;\r\n    temp = *a;\r\n    *a = *b;\r\n    *b = temp;\r\n}\r\n \r\n\/* Function to add a node at the beginning of Linked List *\/\r\nvoid push(struct Node** head_ref, int new_data)\r\n{\r\n    \/* allocate node *\/\r\n    struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));\r\n \r\n    \/* put in the data *\/\r\n    new_node-&gt;data = new_data;\r\n \r\n    \/* link the old list off the new node *\/\r\n    new_node-&gt;next = (*head_ref);\r\n \r\n    \/* move the head to point to the new node *\/\r\n    (*head_ref) = new_node;\r\n}\r\n \r\n\/* Function to print nodes in a given linked list *\/\r\nvoid printList(struct Node* node)\r\n{\r\n    while (node != NULL) {\r\n        printf(&quot;%d &quot;, node-&gt;data);\r\n        node = node-&gt;next;\r\n    }\r\n}\r\n \r\n\/* Driver program to test above function *\/\r\nint main()\r\n{\r\n    struct Node* start = NULL;\r\n \r\n    \/* The constructed linked list is:\r\n    1-&gt;2-&gt;3-&gt;4-&gt;5 *\/\r\n    push(&amp;start, 50);\r\n    push(&amp;start, 40);\r\n    push(&amp;start, 30);\r\n    push(&amp;start, 20);\r\n    push(&amp;start, 10);\r\n \r\n    printf(&quot;Linked list before calling pairWiseSwap()&#92;n&quot;);\r\n    printList(start);\r\n \r\n    pairWiseSwap(start);\r\n \r\n    printf(&quot;&#92;nLinked list after calling pairWiseSwap()&#92;n&quot;);\r\n    printList(start);\r\n \r\n    return 0;\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_3463_2\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"cpp\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include &lt;bits stdc++.h=&quot;&quot;&gt;\r\nusing namespace std;\r\nclass node {\r\npublic:\r\n    int data;\r\n    node* next;\r\n};\r\n\r\nnode* pairWiseSwap(node* head)\r\n{\r\n    if (head == NULL || head-&gt;next == NULL)\r\n        return head;\r\n    node* prev = head;\r\n    node* curr = head-&gt;next;\r\n    head = curr;\r\n    while (true) {\r\n        node* temp = curr-&gt;next;\r\n        curr-&gt;next = prev;\r\n        if (temp == NULL || temp-&gt;next == NULL) {\r\n            prev-&gt;next = temp;\r\n            break;\r\n        }\r\n        prev-&gt;next = temp-&gt;next;\r\n        prev = temp;\r\n        curr = prev-&gt;next;\r\n    }\r\n    return head;\r\n}\r\nvoid push(node** head_ref, int new_data)\r\n{\r\n    node* new_node = new node();\r\n    new_node-&gt;data = new_data;\r\n    new_node-&gt;next = (*head_ref);\r\n    (*head_ref) = new_node;\r\n}\r\nvoid print(node* node)\r\n{\r\n    while (node != NULL) {\r\n        cout &lt;&lt; node-&gt;data &lt;&lt; &quot; &quot;;\r\n        node = node-&gt;next;\r\n    }\r\n}\r\nint main()\r\n{\r\n    node* start = NULL;\r\n    push(&amp;start, 5);\r\n    push(&amp;start, 4);\r\n    push(&amp;start, 3);\r\n    push(&amp;start, 2);\r\n    push(&amp;start, 1);\r\n    start = pairWiseSwap(start);\r\n    print(start);\r\n    return 0;\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_3463_3\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"java\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\nclass PairWise \r\n{\r\n\tNode head;\r\n\tclass Node {\r\n\t\tint data;\r\n\t\tNode next;\r\n\t\tNode(int d)\r\n\t\t{\r\n\t\t\tdata = d;\r\n\t\t\tnext = null;\r\n\t\t}\r\n\t}\r\n\r\n\tvoid pairWiseSwap()\r\n\t{\r\n\t\tNode temp = head;\r\n\r\n\t\t\/* Traverse only till there are atleast 2 nodes left *\/\r\n\t\twhile (temp != null &amp;&amp; temp.next != null) {\r\n\r\n\t\t\t\/* Swap the data *\/\r\n\t\t\tint k = temp.data;\r\n\t\t\ttemp.data = temp.next.data;\r\n\t\t\ttemp.next.data = k;\r\n\t\t\ttemp = temp.next.next;\r\n\t\t}\r\n    }\r\n\tpublic void push(int new_data)\r\n\t{\r\n\t\t\/* 1 &amp; 2: Allocate the Node &amp;\r\n\t\t\t\tPut in the data*\/\r\n\t\tNode new_node = new Node(new_data);\r\n\r\n\t\t\/* 3. Make next of new Node as head *\/\r\n\t\tnew_node.next = head;\r\n\r\n\t\t\/* 4. Move the head to point to new Node *\/\r\n\t\thead = new_node;\r\n\t}\r\n\tvoid printList()\r\n\t{\r\n\t\tNode temp = head;\r\n\t\twhile (temp != null) {\r\n\t\t\tSystem.out.print(temp.data + &quot; &quot;);\r\n\t\t\ttemp = temp.next;\r\n\t\t}\r\n\t\tSystem.out.println();\r\n\t}\r\n\r\n\t\/* Driver program to test above functions *\/\r\n\tpublic static void main(String args[])\r\n\t{\r\n\t\tPairWise llist = new PairWise();\r\n\r\n\t\tllist.push(5);\r\n\t\tllist.push(4);\r\n\t\tllist.push(3);\r\n\t\tllist.push(2);\r\n\t\tllist.push(1);\r\n\r\n\t\tSystem.out.println(&quot;Linked List before calling pairWiseSwap() &quot;);\r\n\t\tllist.printList();\r\n\r\n\t\tllist.pairWiseSwap();\r\n\r\n\t\tSystem.out.println(&quot;Linked List after calling pairWiseSwap() &quot;);\r\n\t\tllist.printList();\r\n\t}\r\n}\r\n\r\n\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_3463_4\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"python\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\nclass Node:\r\n\r\n\tdef __init__(self, data):\r\n\t\tself.data = data\r\n\t\tself.next = None\r\n\r\n\r\nclass LinkedList:\r\n\r\n\tdef __init__(self):\r\n\t\tself.head = None\r\n\r\n\tdef pairwiseSwap(self):\r\n\t\ttemp = self.head\r\n\r\n\t\tif temp is None:\r\n\t\t\treturn\r\n\r\n\t\twhile(temp and temp.next):\r\n\r\n\t\t\tif(temp.data != temp.next.data):\r\n\r\n\t\t\t\ttemp.data, temp.next.data = temp.next.data, temp.data\r\n\r\n\t\t\ttemp = temp.next.next\r\n\r\n\tdef push(self, new_data):\r\n\t\tnew_node = Node(new_data)\r\n\t\tnew_node.next = self.head\r\n\t\tself.head = new_node\r\n\r\n\tdef printList(self):\r\n\t\ttemp = self.head\r\n\t\twhile(temp):\r\n\t\t\tprint(temp.data, end = &quot; &quot;)\r\n\t\t\ttemp = temp.next\r\n\r\n\r\nllist = LinkedList()\r\nllist.push(5)\r\nllist.push(4)\r\nllist.push(3)\r\nllist.push(2)\r\nllist.push(1)\r\n\r\nllist.pairwiseSwap()\r\n\r\nllist.printList()\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_3463 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_3463 a\"),jQuery(\"#tab-content_3463\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t<\/p>\n<p><strong>Output<\/strong>: 2 1 4 3 5<\/p>\n<p><strong>Space Complexity<\/strong>: O(1), constant space required as no extra space is used.<\/p>\n<p>In this blog, we have discussed how to pairwise swap elements of a linked list by changing the links of the nodes directly in the most efficient way. This is one of the most popular interview problems and we have provided the full algorithm with all approaches, and code implementation in different languages with a dry run in picturised form.To practice, similar types of problems check out PrepBytes &#8211; <a href=\"https:\/\/mycode.prepbytes.com\/interview-coding\/practice\/linked-list\">MYCODE | Contest<\/a> Footer<\/p>\n<table width=\"641\">\n<tbody>\n<tr>\n<td colspan=\"2\" width=\"641\" style=\"text-align: center\"><strong>Related Articles<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/doubly-circular-linked-list-introduction-and-insertion\/\">Doubly circular linked list in data structure<\/a><\/td>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/advantages-disadvantages-and-uses-of-a-doubly-linked-list\/\">Application of doubly linked list<\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/applications-of-linked-list-data-structure\/\">Applications of linked list<\/a><\/td>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/difference-between-a-singly-linked-list-and-a-doubly-linked-list\/\">Singly linked list vs doubly linked list<\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/advantage-and-disadvantage-of-linked-list-over-array\/\">Advantages and disadvantages of linked list<\/a><\/td>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/menu-driven-program-for-all-operations-on-doubly-linked-list-in-c\/\">Doubly linked list all operations in C<\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/binary-search\/binary-search-on-linked-list\/\">Binary search in linked list<\/a><\/td>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/bubble-sort-for-linked-list-by-swapping-nodes\/\">Bubble sort linked list<\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/delete-a-node-in-doubly-linked-list\/\">Deletion in doubly linked list<\/a><\/td>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/delete-middle-of-linked-list\/\">Delete the middle node of a linked list<\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/adding-two-polynomials-using-linked-list\/\">Polynomial addition using linked list<\/a><\/td>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/find-the-smallest-and-largest-elements-in-a-singly-linked-list\/\">Find max value and min value in linked list<\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/insert-a-node-at-a-specific-position-in-a-linked-list\/\">Insert a node at a specific position in a linked list<\/a><\/td>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/swap-nodes-in-a-linked-list-without-swapping-data\/\">Swap nodes in linked list<\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/add-two-numbers-represented-by-linked-lists-set-1\/\">Add two numbers represented by linked lists<\/a><\/td>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/find-the-first-node-of-the-loop-in-a-linked-list\/\">Find starting point of loop in linked list<\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/merge-sort-on-a-singly-linked-list\/\">Merge sort linked list<\/a><\/td>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/delete-a-node-at-a-given-position\/\">Delete a node from linked list at a given position<\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/remove-duplicates-from-an-unsorted-linked-list\/\">Remove duplicates from unsorted linked list<\/a><\/td>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/python\/python-program-to-reverse-a-linked-list\/\">Reverse a linked list in Python<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>FAQs to pairwise swap elements of a linked list<\/h2>\n<p><ol><strong><\/p>\n<li>Can we swap on a linked list?<\/li>\n<p><\/strong><br \/>\nWe can swap nodes of the linked list either by swapping their data or by changing the links (swapping nodes)<br \/>\n<strong><\/p>\n<li>Why stability in sorting is important?<\/li>\n<p><\/strong><br \/>\nA stable sorting algorithm maintains the relative order of the items with equal sort keys.<br \/>\n<strong><\/p>\n<li>Why O(1) is fastest?<\/li>\n<p><\/strong><br \/>\nO(1) is faster asymptotically as it is independent of the input<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will understand how to pairwise swap elements of a linked list. Linked list as a sequence of data structures which are connected through links and each node is having a pointer which points to the next node. let\u2019s try how we can pairwise swap elements of a linked list. Problem Statement [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[125],"tags":[],"class_list":["post-3461","post","type-post","status-publish","format-standard","hentry","category-linked-list"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Pairwise swap elements of a given linked list<\/title>\n<meta name=\"description\" content=\"Learn how to pairwise swap elements of a given linked list in the most efficient way possible. This blog explain how to swap pairwise nodes and return the modified linked list.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/the-most-efficient-way-to-pairwise-swap-elements-of-a-given-linked-list\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Pairwise swap elements of a given linked list\" \/>\n<meta property=\"og:description\" content=\"Learn how to pairwise swap elements of a given linked list in the most efficient way possible. This blog explain how to swap pairwise nodes and return the modified linked list.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/the-most-efficient-way-to-pairwise-swap-elements-of-a-given-linked-list\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-08-03T08:59:16+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-18T07:21:07+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644921996084-86%20Pairwise-05.png\" \/>\n<meta name=\"author\" content=\"PrepBytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"PrepBytes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/the-most-efficient-way-to-pairwise-swap-elements-of-a-given-linked-list\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/the-most-efficient-way-to-pairwise-swap-elements-of-a-given-linked-list\/\"},\"author\":{\"name\":\"PrepBytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e\"},\"headline\":\"The most efficient way to Pairwise swap elements of a given linked list\",\"datePublished\":\"2021-08-03T08:59:16+00:00\",\"dateModified\":\"2022-11-18T07:21:07+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/the-most-efficient-way-to-pairwise-swap-elements-of-a-given-linked-list\/\"},\"wordCount\":1036,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/the-most-efficient-way-to-pairwise-swap-elements-of-a-given-linked-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644921996084-86%20Pairwise-05.png\",\"articleSection\":[\"Linked list articles\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/the-most-efficient-way-to-pairwise-swap-elements-of-a-given-linked-list\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/the-most-efficient-way-to-pairwise-swap-elements-of-a-given-linked-list\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/the-most-efficient-way-to-pairwise-swap-elements-of-a-given-linked-list\/\",\"name\":\"Pairwise swap elements of a given linked list\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/the-most-efficient-way-to-pairwise-swap-elements-of-a-given-linked-list\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/the-most-efficient-way-to-pairwise-swap-elements-of-a-given-linked-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644921996084-86%20Pairwise-05.png\",\"datePublished\":\"2021-08-03T08:59:16+00:00\",\"dateModified\":\"2022-11-18T07:21:07+00:00\",\"description\":\"Learn how to pairwise swap elements of a given linked list in the most efficient way possible. This blog explain how to swap pairwise nodes and return the modified linked list.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/the-most-efficient-way-to-pairwise-swap-elements-of-a-given-linked-list\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/the-most-efficient-way-to-pairwise-swap-elements-of-a-given-linked-list\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/the-most-efficient-way-to-pairwise-swap-elements-of-a-given-linked-list\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644921996084-86%20Pairwise-05.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644921996084-86%20Pairwise-05.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/the-most-efficient-way-to-pairwise-swap-elements-of-a-given-linked-list\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linked list articles\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/linked-list\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"The most efficient way to Pairwise swap elements of a given linked list\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e\",\"name\":\"PrepBytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g\",\"caption\":\"PrepBytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/prepbytes\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Pairwise swap elements of a given linked list","description":"Learn how to pairwise swap elements of a given linked list in the most efficient way possible. This blog explain how to swap pairwise nodes and return the modified linked list.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/prepbytes.com\/blog\/the-most-efficient-way-to-pairwise-swap-elements-of-a-given-linked-list\/","og_locale":"en_US","og_type":"article","og_title":"Pairwise swap elements of a given linked list","og_description":"Learn how to pairwise swap elements of a given linked list in the most efficient way possible. This blog explain how to swap pairwise nodes and return the modified linked list.","og_url":"https:\/\/prepbytes.com\/blog\/the-most-efficient-way-to-pairwise-swap-elements-of-a-given-linked-list\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2021-08-03T08:59:16+00:00","article_modified_time":"2022-11-18T07:21:07+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644921996084-86%20Pairwise-05.png","type":"","width":"","height":""}],"author":"PrepBytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"PrepBytes","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/the-most-efficient-way-to-pairwise-swap-elements-of-a-given-linked-list\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/the-most-efficient-way-to-pairwise-swap-elements-of-a-given-linked-list\/"},"author":{"name":"PrepBytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e"},"headline":"The most efficient way to Pairwise swap elements of a given linked list","datePublished":"2021-08-03T08:59:16+00:00","dateModified":"2022-11-18T07:21:07+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/the-most-efficient-way-to-pairwise-swap-elements-of-a-given-linked-list\/"},"wordCount":1036,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/the-most-efficient-way-to-pairwise-swap-elements-of-a-given-linked-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644921996084-86%20Pairwise-05.png","articleSection":["Linked list articles"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/the-most-efficient-way-to-pairwise-swap-elements-of-a-given-linked-list\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/the-most-efficient-way-to-pairwise-swap-elements-of-a-given-linked-list\/","url":"https:\/\/prepbytes.com\/blog\/the-most-efficient-way-to-pairwise-swap-elements-of-a-given-linked-list\/","name":"Pairwise swap elements of a given linked list","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/the-most-efficient-way-to-pairwise-swap-elements-of-a-given-linked-list\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/the-most-efficient-way-to-pairwise-swap-elements-of-a-given-linked-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644921996084-86%20Pairwise-05.png","datePublished":"2021-08-03T08:59:16+00:00","dateModified":"2022-11-18T07:21:07+00:00","description":"Learn how to pairwise swap elements of a given linked list in the most efficient way possible. This blog explain how to swap pairwise nodes and return the modified linked list.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/the-most-efficient-way-to-pairwise-swap-elements-of-a-given-linked-list\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/the-most-efficient-way-to-pairwise-swap-elements-of-a-given-linked-list\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/the-most-efficient-way-to-pairwise-swap-elements-of-a-given-linked-list\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644921996084-86%20Pairwise-05.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644921996084-86%20Pairwise-05.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/the-most-efficient-way-to-pairwise-swap-elements-of-a-given-linked-list\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Linked list articles","item":"https:\/\/prepbytes.com\/blog\/category\/linked-list\/"},{"@type":"ListItem","position":3,"name":"The most efficient way to Pairwise swap elements of a given linked list"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e","name":"PrepBytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g","caption":"PrepBytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/prepbytes\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/3461","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=3461"}],"version-history":[{"count":9,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/3461\/revisions"}],"predecessor-version":[{"id":10581,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/3461\/revisions\/10581"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=3461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=3461"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=3461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}