{"id":1502,"date":"2020-06-11T05:59:12","date_gmt":"2020-06-11T05:59:12","guid":{"rendered":"https:\/\/blog.prepbytes.com\/?p=1502"},"modified":"2022-03-30T22:42:14","modified_gmt":"2022-03-30T22:42:14","slug":"reverse-a-linked-list","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/reverse-a-linked-list\/","title":{"rendered":"REVERSE A LINKED LIST"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645182045491-Article_474.png\" alt=\"\" \/><\/p>\n<h3>Concepts Used<\/h3>\n<blockquote>\n<p>Linked list,recursion.<\/p>\n<\/blockquote>\n<h3>Difficulty Level<\/h3>\n<blockquote>\n<p>Easy.<\/p>\n<\/blockquote>\n<h3>Problem Statement :<\/h3>\n<blockquote>\n<p>Given pointer to the head node of a linked list, the task is to reverse the linked list.<\/p>\n<\/blockquote>\n<p><a href=\"https:\/\/mycode.prepbytes.com\/problems\/linked-list\/RVRSELL\" title=\"Go to mycode.prepbytes.com\" target=\"_blank\" rel=\"noopener noreferrer\"><u><strong><\/strong><\/u><\/a><\/p>\n<h4>Example:<\/h4>\n<pre><code>Assume that we have linked list 1 \u2192 2 \u2192 3 \u2192 \u00d8, \nwe have to change it to \u00d8 \u2190 1 \u2190 2 \u2190 3.<\/code><\/pre>\n<h3>Explanation:<\/h3>\n<h4>Approach 1(Iterative approach):<\/h4>\n<blockquote>\n<p>While you are traversing the list, change the current node&#8217;s next pointer to point to its previous element. Since a node does not have reference to its previous node, you must store its previous element beforehand. You also need another pointer to store the next node before changing the reference. Do not forget to return the new head reference at the end!<\/p>\n<\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2020\/06\/reverse-list.png\" alt=\"\" \/><\/p>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_1504 {\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_1504 .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_1504 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_1504 .wpsm_nav-tabs > li.active > a, #tab_container_1504 .wpsm_nav-tabs > li.active > a:hover, #tab_container_1504 .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_1504 .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_1504 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_1504 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_1504 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_1504 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_1504 .wpsm_nav-tabs > li > a:hover , #tab_container_1504 .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_1504 .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_1504 .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_1504 .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_1504 .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_1504 .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_1504 .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_1504 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_1504 .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_1504 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_1504 .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_1504 .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_1504\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_1504\">\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_1504_1\" aria-controls=\"tabs_desc_1504_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\/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_1504_2\" aria-controls=\"tabs_desc_1504_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>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_1504_3\" aria-controls=\"tabs_desc_1504_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>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_1504\">\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_1504_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\nstruct Node* Reverse(Node *head)\r\n    {\r\n    Node *tail, *t;\r\n    tail = NULL;\r\n    while (head != NULL)\r\n     {\r\n        t = head-&gt;next;\r\n        head-&gt;next = tail;\r\n        tail = head;\r\n        head = t;\r\n    }\r\n    return tail;\r\n     \/\/ Complete this method\r\n       }\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\r\n\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_1504_2\">\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 public static Node reverseLinkedList(Node currentNode)\r\n      {\r\n        \/\/ For first node, previousNode will be null\r\n        Node previousNode=null;\r\n        Node nextNode;\r\n        while(currentNode!=null)\r\n        {\r\n            nextNode=currentNode.next;\r\n            \/\/ reversing the link\r\n            currentNode.next=previousNode;\r\n            \/\/ moving currentNode and previousNode by 1 node\r\n            previousNode=currentNode;\r\n            currentNode=nextNode;\r\n        }\r\n        return previousNode;\r\n      }\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\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_1504_3\">\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\ndef Reverse(head):\r\n    \r\n    tail = NULL\r\n    while (head != NULL):\r\n    \r\n        t = head.next\r\n        head.next = tail\r\n        tail = head\r\n        head = t\r\n    \r\n    return tail\r\n\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\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_1504 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_1504 a\"),jQuery(\"#tab-content_1504\"));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>Approach 2(Using recursion):<\/h4>\n<blockquote>\n<p>We can find it easier to start from the bottom up,by asking and answering tiny questions:<\/p>\n<ol>\n<li>\n<p>What is the reverse of NULL? NULL.<\/p>\n<\/li>\n<li>\n<p>What is the reverse of a one element list?The element itself.<\/p>\n<\/li>\n<li>\n<p>What is the reverse of an n element list? The reverse of the second element followed by the first element.<\/p>\n<\/li>\n<\/ol>\n<\/blockquote>\n<h3>Refer to commented implementation<\/h3>\n<pre><code>     Node Reverse(Node head) {\n\n       \/* We have two conditions in this if statement.\n       This first condition immediately returns null\n       when the list is null. The second condition returns\n        the final node in the list. That final node is sent\n        into the &quot;remaining&quot; Node below.\n        -----------------------------------------------------*\/\n\n       if (head == null || head.next == null) {  \n             return head;  \n         }\n\n     \/* When the recursion creates the stack for A -&gt; B -&gt; C\n       (RevA(RevB(RevC()))) it will stop at the last node and\n        the recursion will end, beginning the unraveling of the\n        nested functions from the inside, out. \n       -----------------------------------------------------*\/\n\n        Node remaining = Reverse(head.next);\n\n     \/* Now we have the &quot;remaining&quot; node returned and accessible\n      to the node prior. This remaining node will be returned\n      by each function as the recursive stack unravels.\n\n      Assigning head to head.next.next where A is the head\n      and B is after A, (A -&gt; B), would set B&#039;s pointer to A,\n      reversing their direction to be A &lt;- B.\n      -----------------------------------------------------*\/\n\n      head.next.next = head; \n\n      \/* Now that those two elements are reversed, we need to set\n      the pointer of the new tail-node to null.\n      -----------------------------------------------------*\/\n\n      head.next = null;  \n\n      \/* Now we return remaining so that remaining is always\n      reassigned to itself and is eventually returned by the\n      first function call.\n      -----------------------------------------------------*\/\n\n      return remaining; \n      }<\/code><\/pre>\n<h3>SOLUTION:<\/h3>\n<p>\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_1503 {\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_1503 .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_1503 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_1503 .wpsm_nav-tabs > li.active > a, #tab_container_1503 .wpsm_nav-tabs > li.active > a:hover, #tab_container_1503 .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_1503 .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_1503 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_1503 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_1503 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_1503 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_1503 .wpsm_nav-tabs > li > a:hover , #tab_container_1503 .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_1503 .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_1503 .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_1503 .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_1503 .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_1503 .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_1503 .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_1503 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_1503 .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_1503 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_1503 .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_1503 .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_1503\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_1503\">\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_1503_1\" aria-controls=\"tabs_desc_1503_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_1503_2\" aria-controls=\"tabs_desc_1503_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_1503_3\" aria-controls=\"tabs_desc_1503_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_1503_4\" aria-controls=\"tabs_desc_1503_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_1503\">\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_1503_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 structure of linked list node *\/\r\nstruct node {\r\n  int data;\r\n  struct node *next;\r\n} *head;\r\n\r\nvoid initialize(){\r\n    head = NULL;\r\n}\r\n\r\n\/* \r\nGiven a Inserts a node in front of a singly linked list. \r\n*\/\r\nvoid insert(int num) {\r\n    \/* Create a new Linked List node *\/\r\n    struct node* newNode = (struct node*) malloc(sizeof(struct node));\r\n    newNode-&gt;data  = num;\r\n    \/* Next pointer of new node will point to head node of linked list  *\/\r\n    newNode-&gt;next = head;\r\n    \/* make new node as new head of linked list *\/\r\n    head = newNode;\r\n    printf(&quot;Inserted Element : %d&#92;n&quot;, num);\r\n}\r\n\r\n\/* Reverses a Linked List using recursion *\/\r\nvoid reverse(struct node* nodePtr) {\r\n      \r\n    \/* empty list *\/\r\n    if (nodePtr == NULL)\r\n       return;   \r\n    \/* Last node (tail node)*\/\r\n    if(nodePtr-&gt;next == NULL){\r\n        head = nodePtr;\r\n        return;   \r\n    }\r\n \r\n    \/* reverse the rest of list and put the first element at the end *\/\r\n    reverse(nodePtr-&gt;next);\r\n    nodePtr-&gt;next-&gt;next = nodePtr;  \r\n    nodePtr-&gt;next = NULL;               \r\n}\r\n\r\n\/*\r\nPrints a linked list from head node till tail node \r\n*\/\r\nvoid printLinkedList(struct node *nodePtr) {\r\n  while (nodePtr != NULL) {\r\n     printf(&quot;%d&quot;, nodePtr-&gt;data);\r\n     nodePtr = nodePtr-&gt;next;\r\n     if(nodePtr != NULL)\r\n         printf(&quot;--&gt;&quot;);\r\n  }\r\n}\r\n \r\nint main() {\r\n    initialize();\r\n    \/* Creating a linked List*\/\r\n    insert(1);  \r\n    insert(2); \r\n    insert(3); \r\n    insert(4);\r\n    insert(5);\r\n    \r\n    printf(&quot;&#92;nLinked List&#92;n&quot;);\r\n    printLinkedList(head);\r\n    \r\n    \/* Reverse Linked List *\/\r\n    reverse(head);\r\n    printf(&quot;&#92;nReversed Linked List&#92;n&quot;);\r\n    printLinkedList(head);\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_1503_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\n     using namespace std;\r\n    struct node{\r\n    int data;\r\n    node* next;\r\n    };\r\n     node* newnode(int x)\r\n    {\r\n    node* temp=new node();\r\n    temp-&gt;data=x;\r\n    temp-&gt;next=NULL;\r\n    return temp;\r\n    }\r\n    node* reverse(node* head)\r\n    {\r\n     if(head==NULL||head-&gt;next==NULL)return head;\r\n    node* restlist=reverse(head-&gt;next);\r\n    head-&gt;next-&gt;next=head;\r\n    head-&gt;next=NULL;\r\n     return restlist;\r\n    }\r\n    int main()\r\n    {\r\n    \/\/write your code here\r\n    int t;cin&gt;&gt;t;\r\n    while(t--)\r\n    {\r\n    int n;\r\n    cin&gt;&gt;n;\r\n    int x;cin&gt;&gt;x;\r\n    node* temp=newnode(x);\r\n    node* head=temp;\r\n    for(int i=1;i&lt;n;i++) {=&quot;&quot; int=&quot;&quot; x;cin=&quot;&quot;&gt;&gt;x;\r\n      head-&gt;next=newnode(x);\r\n      head=head-&gt;next;\r\n    }\r\n    node* head1=reverse(temp);\r\n    while(head1!=NULL)\r\n    {\r\n      cout&lt;&lt;head1-&gt;data&lt;&lt;&quot; &quot;;\r\n      head1=head1-&gt;next;\r\n    }\r\n    cout&lt;&lt;&quot;&#92;n&quot;;\r\n     }\r\n     return 0;\r\n     }\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\r\n\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_1503_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\nimport java.io.*;\r\n     import java.util.*;\r\n    public class Main{\r\n    static class SinglyLinkedListNode {\r\n        public int data;\r\n        public SinglyLinkedListNode next;\r\n        public SinglyLinkedListNode(int nodeData) {\r\n            this.data = nodeData;\r\n            this.next = null;\r\n        }\r\n    }\r\n    static class SinglyLinkedList {\r\n        public SinglyLinkedListNode head;\r\n        public SinglyLinkedListNode tail;\r\n\r\n        public SinglyLinkedList() {\r\n            this.head = null;\r\n            this.tail = null;\r\n        }\r\n\r\n        public void insertNode(int nodeData) {\r\n            SinglyLinkedListNode node = new SinglyLinkedListNode(nodeData);\r\n\r\n            if (this.head == null) {\r\n                this.head = node;\r\n            } else {\r\n                this.tail.next = node;\r\n            }\r\n\r\n            this.tail = node;\r\n        }\r\n    }\r\n    static void printLinkedList(SinglyLinkedListNode head)\r\n    {\r\n        SinglyLinkedListNode temp=head;\r\n        while(temp!=null)\r\n        {\r\n            System.out.print(temp.data+&quot; &quot;);\r\n            temp=temp.next;\r\n        }\r\n        System.out.println();\r\n    }\r\n    static SinglyLinkedListNode reverseLinkedList(SinglyLinkedListNode head) {\r\n        SinglyLinkedListNode prev = null;\r\n        SinglyLinkedListNode current = head;\r\n        SinglyLinkedListNode next = null;\r\n        while (current != null) {\r\n            next = current.next;\r\n            current.next = prev;\r\n            prev = current;\r\n            current = next;\r\n        }\r\n        head = prev;\r\n        return head;\r\n\r\n    }\r\n\r\n    private static final Scanner scanner = new Scanner(System.in);\r\n\r\n    public static void main(String[] args) throws IOException {\r\n        int testCases = scanner.nextInt();\r\n\r\n        while (testCases-- &gt; 0) {\r\n            SinglyLinkedList llist = new SinglyLinkedList();\r\n\r\n            int llistCount = scanner.nextInt();\r\n\r\n            for (int i = 0; i &lt; llistCount; i++) {\r\n                int llistItem = scanner.nextInt();\r\n\r\n                llist.insertNode(llistItem);\r\n            }\r\n\r\n            printLinkedList(reverseLinkedList(llist.head));\r\n\r\n        }\r\n\r\n        scanner.close();\r\n    }\r\n    }\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\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_1503_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\tdef __init__(self):\r\n\t\tself.data = None\r\n\t\tself.next = None\r\n\r\ndef newnode(x):\r\n    \r\n    temp= node()\r\n    temp.data = x\r\n    temp.next = None\r\n    return temp\r\n    \r\n\r\ndef reverse(head):\r\n    \r\n    if(head == None or head.next == None):\r\n    \treturn head\r\n    \r\n    restlist = reverse(head.next)\r\n    head.next.next = head\r\n    head.next = None\r\n    return restlist\r\n\r\nfor _ in range(int(input())):\r\n\r\n\tn = int(input())\r\n\ta = list(map(int,input().split()))\r\n\ttemp = newnode(a[0])\r\n\thead = temp\r\n\r\n\tfor i in range(1, n):\r\n\t\thead.next = newnode(a[i])\r\n\t\thead = head.next\r\n\r\n\thead1 = reverse(temp)\r\n\r\n\twhile head1:\r\n\t\tprint(head1.data, end = &quot; &quot;)\r\n\t\thead1 = head1.next\r\n\tprint()\r\n\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\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_1503 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_1503 a\"),jQuery(\"#tab-content_1503\"));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<br \/>\n[forminator_quiz id=&quot;1506&quot;]<\/p>\n<p>This article tried to discuss the concept of <strong>Linked List, Recursion<\/strong>. Hope this blog helps you understand and solve the problem. To practice more problems on Linked List, Recursion you can check out <a href=\"#\"><\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Concepts Used Linked list,recursion. Difficulty Level Easy. Problem Statement : Given pointer to the head node of a linked list, the task is to reverse the linked list. Example: Assume that we have linked list 1 \u2192 2 \u2192 3 \u2192 \u00d8, we have to change it to \u00d8 \u2190 1 \u2190 2 \u2190 3. [&hellip;]<\/p>\n","protected":false},"author":52,"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":[46],"tags":[],"class_list":["post-1502","post","type-post","status-publish","format-standard","hentry","category-recursion-interview-programming"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Recursion Interview Programming | Reverse a Linked List | Prepbytes<\/title>\n<meta name=\"description\" content=\"Given Pointer to the Head Node of a Linked List, the Task Is to Reverse the Linked List.while You Are Traversing the List, Change the Current Node&#039;s Next Pointer to Point to Its Previous Element.\" \/>\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\/reverse-a-linked-list\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Recursion Interview Programming | Reverse a Linked List | Prepbytes\" \/>\n<meta property=\"og:description\" content=\"Given Pointer to the Head Node of a Linked List, the Task Is to Reverse the Linked List.while You Are Traversing the List, Change the Current Node&#039;s Next Pointer to Point to Its Previous Element.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/reverse-a-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=\"2020-06-11T05:59:12+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-03-30T22:42:14+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645182045491-Article_474.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=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/reverse-a-linked-list\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/reverse-a-linked-list\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"REVERSE A LINKED LIST\",\"datePublished\":\"2020-06-11T05:59:12+00:00\",\"dateModified\":\"2022-03-30T22:42:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/reverse-a-linked-list\/\"},\"wordCount\":208,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/reverse-a-linked-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645182045491-Article_474.png\",\"articleSection\":[\"Recursion Interview Programming\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/reverse-a-linked-list\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/reverse-a-linked-list\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/reverse-a-linked-list\/\",\"name\":\"Recursion Interview Programming | Reverse a Linked List | Prepbytes\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/reverse-a-linked-list\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/reverse-a-linked-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645182045491-Article_474.png\",\"datePublished\":\"2020-06-11T05:59:12+00:00\",\"dateModified\":\"2022-03-30T22:42:14+00:00\",\"description\":\"Given Pointer to the Head Node of a Linked List, the Task Is to Reverse the Linked List.while You Are Traversing the List, Change the Current Node's Next Pointer to Point to Its Previous Element.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/reverse-a-linked-list\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/reverse-a-linked-list\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/reverse-a-linked-list\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645182045491-Article_474.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645182045491-Article_474.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/reverse-a-linked-list\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Recursion Interview Programming\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/recursion-interview-programming\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"REVERSE A 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\/3f7dc4ae851791d5947a7f99df363d5e\",\"name\":\"Prepbytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"caption\":\"Prepbytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Recursion Interview Programming | Reverse a Linked List | Prepbytes","description":"Given Pointer to the Head Node of a Linked List, the Task Is to Reverse the Linked List.while You Are Traversing the List, Change the Current Node's Next Pointer to Point to Its Previous Element.","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\/reverse-a-linked-list\/","og_locale":"en_US","og_type":"article","og_title":"Recursion Interview Programming | Reverse a Linked List | Prepbytes","og_description":"Given Pointer to the Head Node of a Linked List, the Task Is to Reverse the Linked List.while You Are Traversing the List, Change the Current Node's Next Pointer to Point to Its Previous Element.","og_url":"https:\/\/prepbytes.com\/blog\/reverse-a-linked-list\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2020-06-11T05:59:12+00:00","article_modified_time":"2022-03-30T22:42:14+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645182045491-Article_474.png","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/reverse-a-linked-list\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/reverse-a-linked-list\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"REVERSE A LINKED LIST","datePublished":"2020-06-11T05:59:12+00:00","dateModified":"2022-03-30T22:42:14+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/reverse-a-linked-list\/"},"wordCount":208,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/reverse-a-linked-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645182045491-Article_474.png","articleSection":["Recursion Interview Programming"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/reverse-a-linked-list\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/reverse-a-linked-list\/","url":"https:\/\/prepbytes.com\/blog\/reverse-a-linked-list\/","name":"Recursion Interview Programming | Reverse a Linked List | Prepbytes","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/reverse-a-linked-list\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/reverse-a-linked-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645182045491-Article_474.png","datePublished":"2020-06-11T05:59:12+00:00","dateModified":"2022-03-30T22:42:14+00:00","description":"Given Pointer to the Head Node of a Linked List, the Task Is to Reverse the Linked List.while You Are Traversing the List, Change the Current Node's Next Pointer to Point to Its Previous Element.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/reverse-a-linked-list\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/reverse-a-linked-list\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/reverse-a-linked-list\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645182045491-Article_474.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645182045491-Article_474.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/reverse-a-linked-list\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Recursion Interview Programming","item":"https:\/\/prepbytes.com\/blog\/category\/recursion-interview-programming\/"},{"@type":"ListItem","position":3,"name":"REVERSE A 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\/3f7dc4ae851791d5947a7f99df363d5e","name":"Prepbytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","caption":"Prepbytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/1502","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\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=1502"}],"version-history":[{"count":11,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/1502\/revisions"}],"predecessor-version":[{"id":8384,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/1502\/revisions\/8384"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=1502"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=1502"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=1502"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}