{"id":4606,"date":"2021-09-02T08:40:44","date_gmt":"2021-09-02T08:40:44","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=4606"},"modified":"2022-03-10T19:06:12","modified_gmt":"2022-03-10T19:06:12","slug":"iteratively-reverse-a-linked-list-using-only-2-pointers","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/iteratively-reverse-a-linked-list-using-only-2-pointers\/","title":{"rendered":"Iteratively Reverse a Linked List Using Only 2 Pointers"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644913420324-Iteratively%20Reverse%20a%20Linked%20List%20Using%20Only%202%20Pointers_Artboard%203.png\" alt=\"\" \/><\/p>\n<h3>Introduction<\/h3>\n<p>The linked list is one of the most important concepts and data structures to learn while preparing for interviews. Having a good grasp of Linked Lists can be a huge plus point in a coding interview.<\/p>\n<\/p>\n<h3>Problem Statement<\/h3>\n<p>We will be provided with a linked list, we aim to reverse the given linked list, but we are constrained to use only 2 pointers.<\/p>\n<p><strong>Consider Example:<\/strong><br \/>\nInput Linked List:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/input-22.png\" alt=\"\" \/><\/p>\n<p>Output Linked List: <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/output-12.png\" alt=\"\" \/><\/p>\n<h3>Problem Statement Understanding<\/h3>\n<p>In this problem, we are given a Linked List, and we are asked to reverse the provided Linked List. We can get the desired reversed linked list by just <a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/recursive-selection-sort-for-singly-linked-list-swapping-node-links\/\" title=\"swapping\">swapping<\/a> the values of nodes from front to end.<\/p>\n<p><strong>For example:<\/strong><br \/>\nLinked List: head \u2192 1 \u2192 2 \u2192 3 \u2192 4 \u2192 5 \u2192 NULL<\/p>\n<p>So, Essentially, we can just swap the values of node such that:<\/p>\n<ul>\n<li>Node 1&#8217;s data with Node 5&#8217;s data.<\/li>\n<li>Node 2&#8217;s data with Node 4&#8217;s data.<\/li>\n<li>Node 3&#8217;s data is swapped with itself.<\/li>\n<\/ul>\n<p>Our input linked list after swapping the node values will be: head \u2192 5 \u2192 4 \u2192 3 \u2192 2 \u2192 1 \u2192 NULL.<\/p>\n<p>The output linked list which we got after swapping the nodes is basically the reverse of our original linked list. Hence objective achieved.<\/p>\n<p>We have already seen how to reverse a Linked List Using 3 Pointers. To read about the article, you can visit <a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/java-program-to-reverse-a-linked-list\/\">Reverse a Linked List<\/a>.<\/p>\n<p>In this article, we will see how we can achieve the same result using only 2 pointers.<\/p>\n<h4>Helpful Observation<\/h4>\n<ul>\n<li>\n<p>If you could remember from the previous article, where we were using 3 Pointers, you can easily see that these were the 4 lines that were important for manipulating the next pointer of node:<br \/>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_4607 {\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_4607 .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_4607 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_4607 .wpsm_nav-tabs > li.active > a, #tab_container_4607 .wpsm_nav-tabs > li.active > a:hover, #tab_container_4607 .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_4607 .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_4607 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_4607 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_4607 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_4607 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_4607 .wpsm_nav-tabs > li > a:hover , #tab_container_4607 .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_4607 .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_4607 .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_4607 .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_4607 .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_4607 .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_4607 .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_4607 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_4607 .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_4607 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_4607 .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_4607 .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_4607\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_4607\">\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_4607_1\" aria-controls=\"tabs_desc_4607_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_4607_2\" aria-controls=\"tabs_desc_4607_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_4607_3\" aria-controls=\"tabs_desc_4607_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_4607_4\" aria-controls=\"tabs_desc_4607_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_4607\">\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_4607_1\">\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\nwhile(curr != NULL){\r\n    next = curr-&gt;next; \/\/line 1\r\n    curr-&gt;next = prev; \/\/ line 2\r\n    prev = curr; \/\/ line 3\r\n    curr = next; \/\/ line 4\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_4607_2\">\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#include&lt;stdint.h&gt;\r\ntypedef uintptr_t ut;\r\n \r\n\/* Link list node *\/\r\nstruct Node {\r\n    int data;\r\n    struct Node* next;\r\n};\r\n \r\n\/* Function to reverse the linked list using 2 pointers *\/\r\nvoid reverse(struct Node** head_ref)\r\n{\r\n    struct Node* prev = NULL;\r\n    struct Node* current = *head_ref;\r\n \r\n    \/\/ at last prev points to new head\r\n    while (current != NULL) {\r\n        \/\/ This expression evaluates from left to right\r\n        \/\/ current-&gt;next = prev, changes the link from\r\n        \/\/ next to prev node\r\n        \/\/ prev = current, moves prev to current node for\r\n        \/\/ next reversal of node\r\n        \/\/ This example of list will clear it more 1-&gt;2-&gt;3-&gt;4\r\n        \/\/ initially prev = 1, current = 2\r\n        \/\/ Final expression will be current = 1^2^3^2^1,\r\n        \/\/ as we know that bitwise XOR of two same\r\n        \/\/ numbers will always be 0 i.e; 1^1 = 2^2 = 0\r\n        \/\/ After the evaluation of expression current = 3 that\r\n        \/\/ means it has been moved by one node from its\r\n        \/\/ previous position\r\n        current = (struct Node*)((ut)prev ^ (ut)current ^ (ut)(current-&gt;next) ^ (ut)(current-&gt;next = prev) ^ (ut)(prev = current));\r\n    }\r\n \r\n    *head_ref = prev;\r\n}\r\n \r\n\/* Function to push a node *\/\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 linked list *\/\r\nvoid printList(struct Node* head)\r\n{\r\n    struct Node* temp = head;\r\n    while (temp != NULL) {\r\n        printf(&quot;%d  &quot;, temp-&gt;data);\r\n        temp = temp-&gt;next;\r\n    }\r\n}\r\n \r\n\/* Driver program to test above function*\/\r\nint main()\r\n{\r\n    \/* Start with the empty list *\/\r\n    struct Node* head = NULL;\r\n \r\n    push(&amp;head, 20);\r\n    push(&amp;head, 4);\r\n    push(&amp;head, 15);\r\n    push(&amp;head, 85);\r\n \r\n    printf(&quot;Given linked list&#92;n&quot;);\r\n    printList(head);\r\n    reverse(&amp;head);\r\n    printf(&quot;&#92;nReversed Linked list &#92;n&quot;);\r\n    printList(head);\r\n    return 0;\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_4607_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\n\twhile (current != null)\r\n    {\r\n        Node next = current.next; \/\/line 1\r\n        current.next = prev; \/\/line 2\r\n        prev = current; \/\/line 3\r\n        current = next; \/\/line 4\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_4607_4\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"python\"} -->\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\nwhile curr:\r\n\tnext = curr.next\r\n\tcurr.next = prev\r\n\tprev = curr\r\n\tcurr = next\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_4607 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_4607 a\"),jQuery(\"#tab-content_4607\"));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<\/li>\n<li>\n<p>So here you can see that <strong>curr<\/strong> and <strong>prev<\/strong> pointers are definitely needed to reverse the node link. So we cannot eliminate these 2 pointers.<\/p>\n<\/li>\n<li>\n<p>Now the question is can we eliminate <strong>next<\/strong> pointer?<\/p>\n<ul>\n<li>The answer is <strong>YES<\/strong>, you guessed it right! We can eliminate this next Pointer. We will Use XOR to eliminate this pointer. Let&#8217;s see how.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4>Why is XOR Used?<\/h4>\n<p>Let&#8217;s revisit some properties of the xor(^) operator.<\/p>\n<p>Following are two important properties:<\/p>\n<ul>\n<li><strong>X^0 = X<\/strong> (Xor of any element with 0 is the element itself).<\/li>\n<li><strong>X^X = 0<\/strong> (Xor of any element with itself is 0).<\/li>\n<\/ul>\n<p>So, Essentially with this method, we are trying to remove the need for an extra next pointer. We are temporarily storing it using the help of XOR properties.<\/p>\n<h4>How XOR is used?<\/h4>\n<p>We will use the concept of swapping two variables without using a third variable with the help of the xor operator.<\/p>\n<ul>\n<li>\n<p>To swap two variables x and y, without the use of a third variable, do the following :<\/p>\n<ul>\n<li>x = x <strong>xor<\/strong> y<\/li>\n<li>y = x <strong>xor<\/strong> y<\/li>\n<li>x = x <strong>xor<\/strong> y <\/li>\n<\/ul>\n<\/li>\n<li>\n<p>Also, we can write the above three lines in one line.<\/p>\n<ul>\n<li>x = x ^ y ^ (y=x)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><\/p>\n<ul>\n<li>\n<p>Similarly , if we need to swap 3 variables x,y and z, we will be required to use two swaps.<\/p>\n<ul>\n<li>swap(x,y);<\/li>\n<li>swap(y,z);<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>Swapping 3 variables x,y,z using XOR<\/p>\n<ul>\n<li>x = x <strong>xor<\/strong> y <strong>xor<\/strong> z<\/li>\n<li>y = x <strong>xor<\/strong> y <strong>xor<\/strong> z<\/li>\n<li>z = x <strong>xor<\/strong> y <strong>xor<\/strong> z<\/li>\n<li>x = x <strong>xor<\/strong> y <strong>xor<\/strong> z<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>Above solution in One Line. We can use any of these 3 lines to swap x,y,z.<\/p>\n<ul>\n<li>z = x ^ y ^ z ^ (x=y) ^ (y=z)<\/li>\n<li>y = x ^ y ^ z ^ (z=x) ^ (x=y)<\/li>\n<li>x = x ^ y ^ z ^ (y=z) ^ (z=x)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>We are using the same technique to swap node values in our code.<\/p>\n<h3>Dry Run<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/Iteratively-Reverse-a-Linked-List-Using-Only-2-Pointers-1.png\" alt=\"\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/Iteratively-Reverse-a-Linked-List-Using-Only-2-Pointers-2.png\" alt=\"\" \/><\/p>\n<h3>Implementation<\/h3>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_4609 {\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_4609 .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_4609 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_4609 .wpsm_nav-tabs > li.active > a, #tab_container_4609 .wpsm_nav-tabs > li.active > a:hover, #tab_container_4609 .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_4609 .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_4609 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_4609 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_4609 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_4609 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_4609 .wpsm_nav-tabs > li > a:hover , #tab_container_4609 .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_4609 .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_4609 .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_4609 .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_4609 .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_4609 .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_4609 .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_4609 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_4609 .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_4609 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_4609 .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_4609 .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_4609\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_4609\">\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_4609_1\" aria-controls=\"tabs_desc_4609_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_4609_2\" aria-controls=\"tabs_desc_4609_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_4609_3\" aria-controls=\"tabs_desc_4609_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_4609_4\" aria-controls=\"tabs_desc_4609_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_4609\">\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_4609_1\">\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<bits stdc++.h=\"\">\r\nusing namespace std;\r\ntypedef uintptr_t ut;\r\n\r\nstruct Node\r\n{\r\n    int data;\r\n    struct Node *next;\r\n    Node(int x) : data(x) , next(NULL) {}\r\n};\r\n\r\nNode* reverseList(Node* head_ref) {\r\n    Node* curr = head_ref;\r\n    Node* prev = NULL;\r\n    while (curr != nullptr) {\r\n        \/\/Instead of all those 4 lines. We will write the equivalent result in 1 line.\r\n        curr = (struct Node*)((ut)prev^(ut)curr^(ut)(curr->next)^(ut)(curr->next=prev)^(ut)(prev=curr)); \/\/line 5\r\n    }\r\n\r\n    return prev;\r\n}\r\n\r\nvoid printList(Node *node)\r\n{\r\n    while (node != NULL)\r\n    {\r\n        cout << node->data << \" \";\r\n        node = node->next;\r\n    }\r\n}\r\nint main()\r\n{\r\n    Node* head = new Node(1);\r\n    head->next = new Node(2);\r\n    head->next->next = new Node(3);\r\n    head->next->next->next = new Node(4);\r\n    head->next->next->next->next = new Node(5);\r\n\r\n    head = reverseList(head);\r\n    printList(head);\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_4609_2\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"c\"} -->\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 <stdio.h>\r\n#include <stdlib.h>\r\n#include<stdint.h>\r\ntypedef uintptr_t ut;\r\n \r\n\/* Link list node *\/\r\nstruct Node {\r\n    int data;\r\n    struct Node* next;\r\n};\r\n \r\n\/* Function to reverse the linked list using 2 pointers *\/\r\nvoid reverse(struct Node** head_ref)\r\n{\r\n    struct Node* current = *head_ref;\r\n    struct Node* next;\r\n    while (current->next != NULL) {\r\n        next = current->next;\r\n        current->next = next->next;\r\n        next->next = (*head_ref);\r\n        *head_ref = next;\r\n    }\r\n}\r\n \r\n\/* Function to push a node *\/\r\nvoid push(struct Node** head_ref, int new_data)\r\n{\r\n    struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));\r\n    new_node->data = new_data;\r\n    new_node->next = (*head_ref);\r\n    (*head_ref) = new_node;\r\n}\r\n \r\n\/* Function to print linked list *\/\r\nvoid printList(struct Node* head)\r\n{\r\n    struct Node* temp = head;\r\n    while (temp != NULL) {\r\n        printf(\"%d  \", temp->data);\r\n        temp = temp->next;\r\n    }\r\n}\r\n \r\n\/* Driver program to test above function*\/\r\nint main()\r\n{\r\n    \/* Start with the empty list *\/\r\n    struct Node* head = NULL;\r\n \r\n    push(&head, 20);\r\n    push(&head, 4);\r\n    push(&head, 15);\r\n    push(&head, 85);\r\n \r\n    printf(\"Given linked list&#92;n\");\r\n    printList(head);\r\n    reverse(&head);\r\n    printf(\"&#92;nReversed Linked list &#92;n\");\r\n    printList(head);\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_4609_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 IterativeReverse\r\n{\r\n    static class Node\r\n    {\r\n\t    int data;\r\n\t    Node next;\r\n    };\r\n    static Node head_ref = null;\r\n\r\n    static void reverse()\r\n    {\r\n\t    Node prev = null;\r\n\t    Node current = head_ref;\r\n\t\r\n\t    \/\/ At last prev points to new head\r\n\t    while (current != null)\r\n\t    {\r\n\t    \tNode next = current.next;\r\n\t\t    current.next = prev;\r\n\t\t    prev = current;\r\n\t\t    current = next;\r\n\t    }\r\n\t    head_ref = prev;\r\n    }\r\n    static void push(int new_data)\r\n    {\r\n\t    Node new_node = new Node();\r\n\t    new_node.data = new_data;\r\n\t    new_node.next = (head_ref);\r\n\t    (head_ref) = new_node;\r\n    }\r\n    static void printList()\r\n    {\r\n\t    Node temp = head_ref;\r\n\t    while (temp != null)\r\n\t    {\r\n\t\t    System.out.print(temp.data + \" \");\r\n\t\t    temp = temp.next;\r\n\t    }\r\n    }\r\n    public static void main(String []args)\r\n    {\r\n\t    push(1);\r\n\t    push(2);\r\n\t    push(3);\r\n\t    push(4);\r\n\t    push(5);\r\n\r\n\t    System.out.print(\"Given linked list&#92;n\");\r\n\t    printList();\r\n\t    reverse();\r\n\t    System.out.print(\"&#92;nReversed Linked list &#92;n\");\r\n\t    printList();\r\n    }\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_4609_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\n\r\nclass node:A\r\n\r\n\tdef __init__(self, data):\r\n\t\tself.data = data\r\n\t\tself.next = None\r\n\r\nclass LinkedList:\r\n\r\n\tdef __init__(self):\r\n\t\tself.head = None\r\n\r\n\tdef reverse(self):\r\n\t\tprev = None\r\n\t\tcurrent = self.head\r\n\t\twhile(current is not None):\r\n\t\t\tnext, current.next = current.next, prev\r\n\t\t\tprev, current = current, next\r\n\t\tself.head = prev\r\n\t\t\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=\" \")\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.reverse()\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_4609 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_4609 a\"),jQuery(\"#tab-content_4609\"));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\n<h4>Output<\/h4>\n<p>5 4 3 2 1<\/p>\n<p><\/p>\n<p><strong>Now You must be wondering why we typecast the pointers to uintptr_t type?<\/strong><\/p>\n<ul>\n<li>Answer &#8211; In C++, we cannot perform bitwise operations on pointers directly, and uintptr_t is an integer type capable of holding a pointer. So we need to typecast the pointers to uintptr_t to perform xor operations here.<\/li>\n<\/ul>\n<h4>Code Explanation<\/h4>\n<p>Now, I think line 5 in the <strong>reverseList function<\/strong> is somewhat confusing, so let&#8217;s break down this line into 5 components.<\/p>\n<ul>\n<li>prev<\/li>\n<li>curr<\/li>\n<li>curr-&gt;next<\/li>\n<li>curr-&gt;next = prev<\/li>\n<li>prev = curr<\/li>\n<\/ul>\n<p>The expression will be calculated from left to right.<\/p>\n<p>Let us now compare this code with the one we have seen earlier using 3 Pointers. Find Any Similarity?<\/p>\n<ul>\n<li>Actually, yes, this is exactly the same code we have written earlier but made some changes.<\/li>\n<li>Earlier we had <strong>next  = curr\u2192next<\/strong> in line 1, here we&#8217;re temporarily storing <strong>curr\u2192next<\/strong> which is our 3<sup>rd<\/sup> component.<\/li>\n<li>4<sup>th<\/sup> Component is nothing but our line 2 of the previous code.<\/li>\n<li>5<sup>th<\/sup> Component is nothing but our line 3 of the previous code.<\/li>\n<\/ul>\n<p>You must be wondering where line 4 of our previous code has gone? So line 4 was <strong>curr = next<\/strong>. But what does next contain? <\/p>\n<ul>\n<li>It contains <strong>next = curr\u2192next<\/strong>.<\/li>\n<li>Thus, this can be interpreted as <strong>curr = curr-&gt;next<\/strong>, so essentially the result of all these 5 components is <strong>curr\u2192next<\/strong> which will be stored in <strong>curr<\/strong> in LHS.<\/li>\n<\/ul>\n<p><\/p>\n<p><strong>Time Complexity:<\/strong> O(n) where n is the number of nodes in the given LinkedList.<\/p>\n<p>[forminator_quiz id=&#8221;4610&#8243;]<\/p>\n<p>This blog tried to discuss the problem when we are given a linked list and we are asked to reverse it using only 2 pointers. If you want to solve more questions on Linked List, which are curated by our expert mentors at PrepBytes, you can follow this link <a href=\"https:\/\/mycode.prepbytes.com\/interview-coding\/practice\/linked-list\">Linked List<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction The linked list is one of the most important concepts and data structures to learn while preparing for interviews. Having a good grasp of Linked Lists can be a huge plus point in a coding interview. Problem Statement We will be provided with a linked list, we aim to reverse the given linked list, [&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-4606","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>Iteratively Reverse a Linked List Using Only 2 Pointers<\/title>\n<meta name=\"description\" content=\"Given a Linked List, reverse it using only 2 pointers. This blog discuss the problem when we are given a linked list and we are asked to reverse it using only 2 pointers.\" \/>\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\/iteratively-reverse-a-linked-list-using-only-2-pointers\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Iteratively Reverse a Linked List Using Only 2 Pointers\" \/>\n<meta property=\"og:description\" content=\"Given a Linked List, reverse it using only 2 pointers. This blog discuss the problem when we are given a linked list and we are asked to reverse it using only 2 pointers.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/iteratively-reverse-a-linked-list-using-only-2-pointers\/\" \/>\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-09-02T08:40:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-03-10T19:06:12+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644913420324-Iteratively%20Reverse%20a%20Linked%20List%20Using%20Only%202%20Pointers_Artboard%203.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=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/iteratively-reverse-a-linked-list-using-only-2-pointers\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/iteratively-reverse-a-linked-list-using-only-2-pointers\/\"},\"author\":{\"name\":\"PrepBytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e\"},\"headline\":\"Iteratively Reverse a Linked List Using Only 2 Pointers\",\"datePublished\":\"2021-09-02T08:40:44+00:00\",\"dateModified\":\"2022-03-10T19:06:12+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/iteratively-reverse-a-linked-list-using-only-2-pointers\/\"},\"wordCount\":879,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/iteratively-reverse-a-linked-list-using-only-2-pointers\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644913420324-Iteratively%20Reverse%20a%20Linked%20List%20Using%20Only%202%20Pointers_Artboard%203.png\",\"articleSection\":[\"Linked list articles\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/iteratively-reverse-a-linked-list-using-only-2-pointers\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/iteratively-reverse-a-linked-list-using-only-2-pointers\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/iteratively-reverse-a-linked-list-using-only-2-pointers\/\",\"name\":\"Iteratively Reverse a Linked List Using Only 2 Pointers\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/iteratively-reverse-a-linked-list-using-only-2-pointers\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/iteratively-reverse-a-linked-list-using-only-2-pointers\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644913420324-Iteratively%20Reverse%20a%20Linked%20List%20Using%20Only%202%20Pointers_Artboard%203.png\",\"datePublished\":\"2021-09-02T08:40:44+00:00\",\"dateModified\":\"2022-03-10T19:06:12+00:00\",\"description\":\"Given a Linked List, reverse it using only 2 pointers. This blog discuss the problem when we are given a linked list and we are asked to reverse it using only 2 pointers.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/iteratively-reverse-a-linked-list-using-only-2-pointers\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/iteratively-reverse-a-linked-list-using-only-2-pointers\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/iteratively-reverse-a-linked-list-using-only-2-pointers\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644913420324-Iteratively%20Reverse%20a%20Linked%20List%20Using%20Only%202%20Pointers_Artboard%203.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644913420324-Iteratively%20Reverse%20a%20Linked%20List%20Using%20Only%202%20Pointers_Artboard%203.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/iteratively-reverse-a-linked-list-using-only-2-pointers\/#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\":\"Iteratively Reverse a Linked List Using Only 2 Pointers\"}]},{\"@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":"Iteratively Reverse a Linked List Using Only 2 Pointers","description":"Given a Linked List, reverse it using only 2 pointers. This blog discuss the problem when we are given a linked list and we are asked to reverse it using only 2 pointers.","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\/iteratively-reverse-a-linked-list-using-only-2-pointers\/","og_locale":"en_US","og_type":"article","og_title":"Iteratively Reverse a Linked List Using Only 2 Pointers","og_description":"Given a Linked List, reverse it using only 2 pointers. This blog discuss the problem when we are given a linked list and we are asked to reverse it using only 2 pointers.","og_url":"https:\/\/prepbytes.com\/blog\/iteratively-reverse-a-linked-list-using-only-2-pointers\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2021-09-02T08:40:44+00:00","article_modified_time":"2022-03-10T19:06:12+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644913420324-Iteratively%20Reverse%20a%20Linked%20List%20Using%20Only%202%20Pointers_Artboard%203.png","type":"","width":"","height":""}],"author":"PrepBytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"PrepBytes","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/iteratively-reverse-a-linked-list-using-only-2-pointers\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/iteratively-reverse-a-linked-list-using-only-2-pointers\/"},"author":{"name":"PrepBytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e"},"headline":"Iteratively Reverse a Linked List Using Only 2 Pointers","datePublished":"2021-09-02T08:40:44+00:00","dateModified":"2022-03-10T19:06:12+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/iteratively-reverse-a-linked-list-using-only-2-pointers\/"},"wordCount":879,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/iteratively-reverse-a-linked-list-using-only-2-pointers\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644913420324-Iteratively%20Reverse%20a%20Linked%20List%20Using%20Only%202%20Pointers_Artboard%203.png","articleSection":["Linked list articles"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/iteratively-reverse-a-linked-list-using-only-2-pointers\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/iteratively-reverse-a-linked-list-using-only-2-pointers\/","url":"https:\/\/prepbytes.com\/blog\/iteratively-reverse-a-linked-list-using-only-2-pointers\/","name":"Iteratively Reverse a Linked List Using Only 2 Pointers","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/iteratively-reverse-a-linked-list-using-only-2-pointers\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/iteratively-reverse-a-linked-list-using-only-2-pointers\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644913420324-Iteratively%20Reverse%20a%20Linked%20List%20Using%20Only%202%20Pointers_Artboard%203.png","datePublished":"2021-09-02T08:40:44+00:00","dateModified":"2022-03-10T19:06:12+00:00","description":"Given a Linked List, reverse it using only 2 pointers. This blog discuss the problem when we are given a linked list and we are asked to reverse it using only 2 pointers.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/iteratively-reverse-a-linked-list-using-only-2-pointers\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/iteratively-reverse-a-linked-list-using-only-2-pointers\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/iteratively-reverse-a-linked-list-using-only-2-pointers\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644913420324-Iteratively%20Reverse%20a%20Linked%20List%20Using%20Only%202%20Pointers_Artboard%203.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644913420324-Iteratively%20Reverse%20a%20Linked%20List%20Using%20Only%202%20Pointers_Artboard%203.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/iteratively-reverse-a-linked-list-using-only-2-pointers\/#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":"Iteratively Reverse a Linked List Using Only 2 Pointers"}]},{"@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\/4606","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=4606"}],"version-history":[{"count":7,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/4606\/revisions"}],"predecessor-version":[{"id":7954,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/4606\/revisions\/7954"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=4606"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=4606"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=4606"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}