{"id":5085,"date":"2021-09-20T09:00:00","date_gmt":"2021-09-20T09:00:00","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=5085"},"modified":"2023-07-27T12:47:22","modified_gmt":"2023-07-27T12:47:22","slug":"reverse-a-doubly-linked-list-2","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/reverse-a-doubly-linked-list-2\/","title":{"rendered":"Reverse a Doubly Linked List"},"content":{"rendered":"<p>x<img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645007326243-Article_154.png\" alt=\"\" \/><\/p>\n<p>Linked list is one of the most famous and important data structures asked in most interviews. In this article, we will learn how to reverse a doubly linked list<\/p>\n<p>A doubly linked list is a data structure which contains nodes with points to the previous node as well to the next node.<\/p>\n<h2>How to Reverse a Doubly Linked List<\/h2>\n<p>In this problem, we will be given a doubly linked list as input, and we need to reverse it such that the pointer currently pointing to the head of the list will point to the tail of the doubly linked list.<\/p>\n<p>According to the problem statement, we will be given a doubly-linked list, and we need to reverse a doubly linked list. <\/p>\n<p>Let&#8217;s understand this problem with the help of some examples.<\/p>\n<p>If the given doubly linked list is:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/input-17.png\" alt=\"\" \/><\/p>\n<ul>\n<li>According to the problem statement we need to reverse this given doubly linked list such that after reversing the doubly linked list will look like:<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/output-16.png\" alt=\"\" \/><\/p>\n<p>Taking another example, if the given list is head -&gt; 1  2  3  5  6 -&gt; NULL.<\/p>\n<ul>\n<li>In this case after reversing a doubly linked list our list will look like: head -&gt; 6  5  3  2  1 -&gt; NULL.<\/li>\n<\/ul>\n<p><strong>Some more examples<\/strong><\/p>\n<p>Sample Input 1: 1  3  5  7  11 -&gt; NULL.<br \/>\nSample Output 1: 11  7  5  3  1 -&gt; NULL.<\/p>\n<p>Sample Input 2: 2  3  5  7  11  17 -&gt; NULL.<br \/>\nSample Output 2: 17  11  7  5  3  2 -&gt; NULL.<\/p>\n<p>Now, I think from the above examples, the problem statement is clear. Let\u2019s see how we can approach it.<\/p>\n<p>Before moving to the approach section, try to think about how you can reverse a doubly linked list.<\/p>\n<ul>\n<li>If stuck, no problem, we will thoroughly see how we can reverse a doubly linked list in the next section.<\/li>\n<\/ul>\n<p>Let\u2019s move to the approach section.<\/p>\n<h3>Approach 1 : Using 2 Pointers<\/h3>\n<p>This approach will be similar to reversing data present in an array.<br \/>\n1) We will initialize 2 pointers, one at starting and the other at the end of the linked list.<br \/>\n2) We will swap the node&#8217;s data to which the pointers are pointing.<br \/>\n3) And then, we will move the pointers toward&#8217;s each other by one step.<br \/>\n4) We will be performing step 2 and step 3 untill both the pointers do not cross each other or reach at the same position.<br \/>\n5) Finally, when both pointers reaches the same position, our doubly linked list will be fully reversed.<\/p>\n<p><strong>Time Complexity:<\/strong> O(n), 2 traversals will be required in this approach, one to get the address of the last node and another traversal to reverse the list.<\/p>\n<p><strong>Space Complexity:<\/strong> O(1)<\/p>\n<p>The above approach will work fine, but the only issue is that we are traversing the doubly linked list twice. Now we need to ask ourselves one simple question: Do we actually need two list traversals to reverse the doubly linked list.<\/p>\n<p>Is it possible to reverse a doubly linked list in a single traversal?<\/p>\n<ul>\n<li>Yes, it is possible, and we will see it in the following approach.<\/li>\n<\/ul>\n<p>Let&#8217;s move to next approach.<\/p>\n<h3>Approach 2: Swapping<\/h3>\n<p>Because we can access both the previous and next node of a given node in a doubly linked list, we can easily achieve list reversal by swapping the previous and next pointers of all nodes iteratively.<\/p>\n<p>Let&#8217;s move to the algorithm section to see the above approach in depth.<\/p>\n<h3>Algorithm<\/h3>\n<ul>\n<li>Create a pointer <strong>current<\/strong>. Initialize it with the head of the linked list.<\/li>\n<li>Create a pointer <strong>temp<\/strong>. Initialize it with NULL.<\/li>\n<li>Now swap <strong>current&#8217;s prev<\/strong> and <strong>current&#8217;s next<\/strong> using pointer <strong>temp<\/strong>.<\/li>\n<li>Now move <strong>current<\/strong> to <strong>current&#8217;s previous<\/strong> node because after swapping, <strong>current&#8217;s prev<\/strong> stores the address of <strong>current&#8217;s next<\/strong> node.<\/li>\n<li>Repeat the <strong>process<\/strong> till <strong>current<\/strong> does not become NULL.<\/li>\n<\/ul>\n<h3>Dry run<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/p_1-13.png\" alt=\"\" \/><br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/p_2-14.png\" alt=\"\" \/><\/p>\n<h3>Code Implementation to Reverse a Doubly Linked List:<\/h3>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_5086 {\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_5086 .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_5086 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_5086 .wpsm_nav-tabs > li.active > a, #tab_container_5086 .wpsm_nav-tabs > li.active > a:hover, #tab_container_5086 .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_5086 .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_5086 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_5086 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_5086 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_5086 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_5086 .wpsm_nav-tabs > li > a:hover , #tab_container_5086 .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_5086 .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_5086 .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_5086 .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_5086 .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_5086 .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_5086 .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_5086 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_5086 .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_5086 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_5086 .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_5086 .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_5086\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_5086\">\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_5086_1\" aria-controls=\"tabs_desc_5086_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_5086_2\" aria-controls=\"tabs_desc_5086_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_5086_3\" aria-controls=\"tabs_desc_5086_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_5086_4\" aria-controls=\"tabs_desc_5086_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_5086\">\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_5086_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 node of the doubly linked list *\/\r\nstruct Node\r\n{\r\n  int data;\r\n  struct Node *next;\r\n  struct Node *prev;   \r\n};\r\n \r\n\/* Function to reverse a Doubly Linked List *\/\r\nvoid reverse(struct Node **head_ref)\r\n{\r\n     struct Node *temp = NULL; \r\n     struct Node *current = *head_ref;\r\n      \r\n     while (current !=  NULL)\r\n     {\r\n       temp = current-&gt;prev;\r\n       current-&gt;prev = current-&gt;next;\r\n       current-&gt;next = temp;             \r\n       current = current-&gt;prev;\r\n     }     \r\n      \r\n     if(temp != NULL )\r\n        *head_ref = temp-&gt;prev;\r\n}    \r\n \r\n \r\n \r\n\/* UTILITY FUNCTIONS *\/\r\n\/* Function to insert a node at the beginning of the Doubly Linked List *\/\r\nvoid push(struct Node** head_ref, int new_data)\r\n{\r\n    struct Node* new_node =\r\n            (struct Node*) malloc(sizeof(struct Node));\r\n  \r\n    new_node-&gt;data  = new_data;\r\n    \r\n    new_node-&gt;prev = NULL;\r\n  \r\n    new_node-&gt;next = (*head_ref);   \r\n \r\n    if((*head_ref) !=  NULL)\r\n      (*head_ref)-&gt;prev = new_node ;   \r\n  \r\n    (*head_ref)    = new_node;\r\n}\r\n \r\n\/* Function to print nodes in a given doubly linked list\r\n   This function is same as printList() of singly linked list *\/\r\nvoid printList(struct Node *node)\r\n{\r\n  while(node!=NULL)\r\n  {\r\n   printf(&quot;%d &quot;, node-&gt;data);\r\n   node = node-&gt;next;\r\n  }\r\n}\r\n \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, 2);\r\n  push(&amp;head, 7);\r\n  push(&amp;head, 1);\r\n  push(&amp;head, 13);\r\n  push(&amp;head, 17);\r\n  \r\n  printf(&quot;&#92;n Initial linked list before reversing: &quot;);\r\n  printList(head);\r\n  \r\n  reverse(&amp;head);\r\n  \r\n  printf(&quot;&#92;n Resultant reversed linked list: &quot;);\r\n  printList(head);          \r\n  \r\n  getchar();\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_5086_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\r\n#include &lt;bits\/stdc++.h&gt;\r\nusing namespace std;\r\n\r\n\/* Node structure of a doubly linked list node *\/\r\nclass Node\r\n{\r\n    public:\r\n    int data;\r\n    Node* next;\r\n    Node* prev;\r\n};\r\n\r\n\/* Using this below function we will be adding a new node at beginning of the list *\/\r\nvoid add_at_begin(Node** head, int x)\r\n{\r\n \r\n    Node* new_node = new Node();\r\n\r\n    new_node-&gt;data = x;\r\n \r\n    new_node-&gt;prev = NULL;\r\n \r\n    new_node-&gt;next = (*head);\r\n    \r\n    if ((*head) != NULL)\r\n        (*head)-&gt;prev = new_node;\r\n \r\n    (*head) = new_node;\r\n}\r\n\r\n\/* Using this below function we will be printing the content of the linked list *\/\r\nvoid print_list(Node* node)\r\n{\r\n    while (node != NULL)\r\n    {\r\n        cout &lt;&lt; node-&gt;data &lt;&lt; &quot;,&quot;;\r\n        node = node-&gt;next;\r\n    }\r\n    cout&lt;&lt;endl;\r\n}\r\n\r\n\/* Using this below function we will be reversing the given doubly linked list *\/\r\nvoid reverse_list(Node **head)\r\n{\r\n    Node *temp = NULL;\r\n    Node *current = *head;\r\n    \r\n    while (current != NULL)\r\n    {\r\n        temp = current-&gt;prev;\r\n        current-&gt;prev = current-&gt;next;\r\n        current-&gt;next = temp;            \r\n        current = current-&gt;prev;\r\n    }\r\n     \r\n    if(temp != NULL )\r\n        *head = temp-&gt;prev;\r\n}\r\n \r\n\/* Main function *\/\r\nint main()\r\n{\r\n    Node* head = NULL;\r\n \r\n    add_at_begin(&amp;head, 17);\r\n    add_at_begin(&amp;head, 13);\r\n    add_at_begin(&amp;head, 1);\r\n    add_at_begin(&amp;head, 7);\r\n    add_at_begin(&amp;head, 2);\r\n \r\n    cout&lt;&lt;&quot;Initial linked list before reversing: &quot;&lt;&lt;endl;\r\n    print_list(head);\r\n\r\n    reverse_list(&amp;head);\r\n    \r\n    cout&lt;&lt;&quot;Resultant reversed linked list: &quot;&lt;&lt;endl;\r\n    print_list(head);\r\n \r\n    return 0;\r\n}\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_5086_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 DoublyLinkedList {\r\n \r\n    static Node head;\r\n \r\n    static class Node {\r\n \r\n        int data;\r\n        Node next, prev;\r\n \r\n        Node(int d)\r\n        {\r\n            data = d;\r\n            next = prev = null;\r\n        }\r\n    }\r\n \r\n    \/* Function to reverse a Doubly Linked List *\/\r\n    void reverse_list()\r\n    {\r\n        Node temp = null;\r\n        Node current = head;\r\n \r\n        \/* swap next and prev for all nodes of\r\n         doubly linked list *\/\r\n        while (current != null) {\r\n            temp = current.prev;\r\n            current.prev = current.next;\r\n            current.next = temp;\r\n            current = current.prev;\r\n        }\r\n \r\n        \/* Before changing head, check for the cases like\r\n         empty list and list with only one node *\/\r\n        if (temp != null) {\r\n            head = temp.prev;\r\n        }\r\n    }\r\n    \/* Function to insert a node at the beginning of the\r\n     * Doubly Linked List *\/\r\n    void add_at_begin(int new_data)\r\n    {\r\n        Node new_node = new Node(new_data);\r\n \r\n        new_node.prev = null;\r\n \r\n        new_node.next = head;\r\n \r\n        if (head != null) {\r\n            head.prev = new_node;\r\n        }\r\n        head = new_node;\r\n    }\r\n    void print_list(Node node)\r\n    {\r\n        while (node != null) {\r\n            System.out.print(node.data + &quot; &quot;);\r\n            node = node.next;\r\n        }\r\n    }\r\n \r\n    public static void main(String[] args)\r\n    {\r\n        DoublyLinkedList list = new DoublyLinkedList();\r\n \r\n        list.add_at_begin(17);\r\n    \tlist.add_at_begin(13);\r\n    \tlist.add_at_begin(1);\r\n    \tlist.add_at_begin(7);\r\n    \tlist.add_at_begin(2);\r\n \r\n        System.out.println(&quot;Original linked list &quot;);\r\n        list.print_list(head);\r\n \r\n        list.reverse_list();\r\n        System.out.println(&quot;&quot;);\r\n        System.out.println(&quot;The reversed Linked List is &quot;);\r\n        list.print_list(head);\r\n    }\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_5086_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:\r\n\t# Node structure of a doubly linked list node\r\n\tdef __init__(self, data):\r\n\t\tself.data = data\r\n\t\tself.next = None\r\n\t\tself.prev = None\r\n\r\n\r\nclass DoublyLinkedList:\r\n\tdef __init__(self):\r\n\t\tself.head = None\r\n\r\n\t# Function reverse a Doubly Linked List\r\n\tdef reverse_list(self):\r\n\t\ttemp = None\r\n\t\tcurrent = self.head\r\n\r\n\t\twhile current is not None:\r\n\t\t\ttemp = current.prev\r\n\t\t\tcurrent.prev = current.next\r\n\t\t\tcurrent.next = temp\r\n\t\t\tcurrent = current.prev\r\n\r\n\t\tif temp is not None:\r\n\t\t\tself.head = temp.prev\r\n\r\n\t# Using this below function we will be adding a new node at beginning of the list\r\n\tdef add_at_begin(self, x):\r\n\r\n\t\tnew_node = Node(x)\r\n\r\n\t\tnew_node.prev = None\r\n\r\n\t\tnew_node.next = self.head\r\n\r\n\t\tif self.head is not None:\r\n\t\t\tself.head.prev = new_node\r\n\r\n\t\tself.head = new_node\r\n\r\n\t# Using this below function we will be printing the content of the linked list\r\n\tdef print_list(self, node):\r\n\t\twhile(node is not None):\r\n\t\t\tprint(node.data,end=',')\r\n\t\t\tnode = node.next\r\n\r\n\r\n# main code\r\ndll = DoublyLinkedList()\r\ndll.add_at_begin(17)\r\ndll.add_at_begin(13)\r\ndll.add_at_begin(1)\r\ndll.add_at_begin(7)\r\ndll.add_at_begin(2)\r\n\r\nprint (&quot;Initial linked list before reversing: &quot;)\r\ndll.print_list(dll.head)\r\n\r\n# Reverse doubly linked list\r\ndll.reverse_list()\r\n\r\nprint (&quot;Resultant reversed linked list: &quot;)\r\ndll.print_list(dll.head)\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_5086 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_5086 a\"),jQuery(\"#tab-content_5086\"));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>Initial linked list before reversing:<br \/>\n2,7,1,13,17,<br \/>\nResultant reversed linked list:<br \/>\n17,13,1,7,2,<\/p>\n<p><strong>Time Complexity:<\/strong> O(n), where n is the number of nodes in the linked list.<\/p>\n<p><strong>Conclusion<\/strong><br \/>\nReversing a doubly linked list is an essential operation that involves reversing the direction of pointers in each node, resulting in a complete reversal of the list. We explored two approaches to achieve this task. The first approach, using two pointers, requires two traversals of the list to swap the nodes&#8217; data. On the other hand, the second approach, which involves swapping the previous and next pointers of each node iteratively, accomplishes the reversal in a single pass. Both approaches have a time complexity of O(n), where n is the number of nodes in the linked list. However, the second approach, using swapping, is more efficient as it performs the reversal with only one traversal, reducing the overall time complexity. Reversing a doubly linked list is a crucial operation in linked list manipulation and is often used in various algorithms and data structure implementations. By understanding and applying these techniques, developers can efficiently reverse a doubly linked list and manipulate the data structure according to their specific needs.<\/p>\n<h2>Frequently Asked Questions (FAQs)<\/h2>\n<p>Here are some of the frequently asked questions on how to reverse doubly linked list<\/p>\n<p><strong>Q1. What is a doubly linked list?<\/strong><br \/>\nA doubly linked list is a data structure that contains nodes that point to both the previous and next node.<\/p>\n<p><strong>Q2. What is the time complexity for reversing a doubly linked list?<\/strong><br \/>\nReversing a doubly linked list has a time complexity of O(1).<\/p>\n<p><strong>Q3. Can we reverse a doubly linked list in less than O(N)?<\/strong><br \/>\nBecause we need to traverse to the end to find the tail node, we can reverse a doubly linked list in less than O(N) time.<\/p>\n<table width=\"641\">\n<tbody>\n<tr>\n<td colspan=\"2\" width=\"641\" style=\"text-align: center\"><strong>Related Articles<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/doubly-circular-linked-list-introduction-and-insertion\/\">Doubly circular linked list in data structure<\/a><\/td>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/advantages-disadvantages-and-uses-of-a-doubly-linked-list\/\">Application of doubly linked list<\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/applications-of-linked-list-data-structure\/\">Applications of linked list<\/a><\/td>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/difference-between-a-singly-linked-list-and-a-doubly-linked-list\/\">Singly linked list vs doubly linked list<\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/advantage-and-disadvantage-of-linked-list-over-array\/\">Advantages and disadvantages of linked list<\/a><\/td>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/menu-driven-program-for-all-operations-on-doubly-linked-list-in-c\/\">Doubly linked list all operations in C<\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/binary-search\/binary-search-on-linked-list\/\">Binary search in linked list<\/a><\/td>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/bubble-sort-for-linked-list-by-swapping-nodes\/\">Bubble sort linked list<\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/delete-a-node-in-doubly-linked-list\/\">Deletion in doubly linked list<\/a><\/td>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/delete-middle-of-linked-list\/\">Delete the middle node of a linked list<\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/adding-two-polynomials-using-linked-list\/\">Polynomial addition using linked list<\/a><\/td>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/find-the-smallest-and-largest-elements-in-a-singly-linked-list\/\">Find max value and min value in linked list<\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/insert-a-node-at-a-specific-position-in-a-linked-list\/\">Insert a node at a specific position in a linked list<\/a><\/td>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/swap-nodes-in-a-linked-list-without-swapping-data\/\">Swap nodes in linked list<\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/add-two-numbers-represented-by-linked-lists-set-1\/\">Add two numbers represented by linked lists<\/a><\/td>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/find-the-first-node-of-the-loop-in-a-linked-list\/\">Find starting point of loop in linked list<\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/merge-sort-on-a-singly-linked-list\/\">Merge sort linked list<\/a><\/td>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/delete-a-node-at-a-given-position\/\">Delete a node from linked list at a given position<\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/remove-duplicates-from-an-unsorted-linked-list\/\">Remove duplicates from unsorted linked list<\/a><\/td>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/python\/python-program-to-reverse-a-linked-list\/\">Reverse a linked list in Python<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>x Linked list is one of the most famous and important data structures asked in most interviews. In this article, we will learn how to reverse a doubly linked list A doubly linked list is a data structure which contains nodes with points to the previous node as well to the next node. How to [&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-5085","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>How to Reverse a Doubly Linked List | Reversing a Doubly Linked List<\/title>\n<meta name=\"description\" content=\"Learn the methods (Using two pointers and Swaping the nodes) to reverse a doubly linked list with the code implementation.\" \/>\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-doubly-linked-list-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Reverse a Doubly Linked List | Reversing a Doubly Linked List\" \/>\n<meta property=\"og:description\" content=\"Learn the methods (Using two pointers and Swaping the nodes) to reverse a doubly linked list with the code implementation.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/reverse-a-doubly-linked-list-2\/\" \/>\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-20T09:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-07-27T12:47:22+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645007326243-Article_154.png\" \/>\n<meta name=\"author\" content=\"PrepBytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"PrepBytes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/reverse-a-doubly-linked-list-2\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/reverse-a-doubly-linked-list-2\/\"},\"author\":{\"name\":\"PrepBytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e\"},\"headline\":\"Reverse a Doubly Linked List\",\"datePublished\":\"2021-09-20T09:00:00+00:00\",\"dateModified\":\"2023-07-27T12:47:22+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/reverse-a-doubly-linked-list-2\/\"},\"wordCount\":1057,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/reverse-a-doubly-linked-list-2\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645007326243-Article_154.png\",\"articleSection\":[\"Linked list articles\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/reverse-a-doubly-linked-list-2\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/reverse-a-doubly-linked-list-2\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/reverse-a-doubly-linked-list-2\/\",\"name\":\"How to Reverse a Doubly Linked List | Reversing a Doubly Linked List\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/reverse-a-doubly-linked-list-2\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/reverse-a-doubly-linked-list-2\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645007326243-Article_154.png\",\"datePublished\":\"2021-09-20T09:00:00+00:00\",\"dateModified\":\"2023-07-27T12:47:22+00:00\",\"description\":\"Learn the methods (Using two pointers and Swaping the nodes) to reverse a doubly linked list with the code implementation.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/reverse-a-doubly-linked-list-2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/reverse-a-doubly-linked-list-2\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/reverse-a-doubly-linked-list-2\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645007326243-Article_154.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645007326243-Article_154.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/reverse-a-doubly-linked-list-2\/#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\":\"Reverse a Doubly Linked List\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e\",\"name\":\"PrepBytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g\",\"caption\":\"PrepBytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/prepbytes\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Reverse a Doubly Linked List | Reversing a Doubly Linked List","description":"Learn the methods (Using two pointers and Swaping the nodes) to reverse a doubly linked list with the code implementation.","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-doubly-linked-list-2\/","og_locale":"en_US","og_type":"article","og_title":"How to Reverse a Doubly Linked List | Reversing a Doubly Linked List","og_description":"Learn the methods (Using two pointers and Swaping the nodes) to reverse a doubly linked list with the code implementation.","og_url":"https:\/\/prepbytes.com\/blog\/reverse-a-doubly-linked-list-2\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2021-09-20T09:00:00+00:00","article_modified_time":"2023-07-27T12:47:22+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645007326243-Article_154.png","type":"","width":"","height":""}],"author":"PrepBytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"PrepBytes","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/reverse-a-doubly-linked-list-2\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/reverse-a-doubly-linked-list-2\/"},"author":{"name":"PrepBytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e"},"headline":"Reverse a Doubly Linked List","datePublished":"2021-09-20T09:00:00+00:00","dateModified":"2023-07-27T12:47:22+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/reverse-a-doubly-linked-list-2\/"},"wordCount":1057,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/reverse-a-doubly-linked-list-2\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645007326243-Article_154.png","articleSection":["Linked list articles"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/reverse-a-doubly-linked-list-2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/reverse-a-doubly-linked-list-2\/","url":"https:\/\/prepbytes.com\/blog\/reverse-a-doubly-linked-list-2\/","name":"How to Reverse a Doubly Linked List | Reversing a Doubly Linked List","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/reverse-a-doubly-linked-list-2\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/reverse-a-doubly-linked-list-2\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645007326243-Article_154.png","datePublished":"2021-09-20T09:00:00+00:00","dateModified":"2023-07-27T12:47:22+00:00","description":"Learn the methods (Using two pointers and Swaping the nodes) to reverse a doubly linked list with the code implementation.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/reverse-a-doubly-linked-list-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/reverse-a-doubly-linked-list-2\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/reverse-a-doubly-linked-list-2\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645007326243-Article_154.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645007326243-Article_154.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/reverse-a-doubly-linked-list-2\/#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":"Reverse a Doubly Linked List"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e","name":"PrepBytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g","caption":"PrepBytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/prepbytes\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/5085","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=5085"}],"version-history":[{"count":9,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/5085\/revisions"}],"predecessor-version":[{"id":17390,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/5085\/revisions\/17390"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=5085"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=5085"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=5085"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}