{"id":5089,"date":"2021-09-20T07:42:34","date_gmt":"2021-09-20T07:42:34","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=5089"},"modified":"2023-11-28T04:56:59","modified_gmt":"2023-11-28T04:56:59","slug":"merge-two-sorted-linked-lists-such-that-the-merged-list-is-in-reverse-order","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/merge-two-sorted-linked-lists-such-that-the-merged-list-is-in-reverse-order\/","title":{"rendered":"Merge two sorted linked lists such that the merged list is in reverse order"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645007374578-Article_155.png\" alt=\"\" \/><\/p>\n<p>Welcome to the realm of linked list manipulation! If you&#8217;ve ever wondered how to efficiently merge two sorted linked lists in such a way that the resulting list is in reverse order, you&#8217;re about to embark on a fascinating journey. Merging linked lists is a common task in programming, but when the requirement is to have the merged list in reverse order, it adds an intriguing twist. In this guide, we&#8217;ll delve into the techniques and algorithms to seamlessly merge two sorted linked lists while maintaining that reversed order. Let&#8217;s unravel the steps to achieve this and explore the intricacies of linked list manipulation.<\/p>\n<h2>How To Merge 2 Sorted Linked List In Reverse Order<\/h2>\n<p>In this problem, we are given two sorted linked lists, and we are asked to merge the two lists in such a way that the new merged list is in reverse order.<\/p>\n<p>Let\u2019s try to understand this problem with the help of examples.<br \/>\nIf the given linked lists are:<br \/>\n<strong>list 1:<\/strong> 3\u21927\u219210\u219214.<br \/>\n<strong>list 2:<\/strong> 12\u219213\u219215.<\/p>\n<ul>\n<li>According to the problem statement, we need to merge the list 1 and list 2 in such a way that the final merged list is in the reverse order.<\/li>\n<li>When we actually merge the list 1 and list 2, our merged list will be 3\u21927\u219210\u219212\u219213\u219214\u219215.<\/li>\n<li>And our output will be the reverse of this merged list, which is 15\u219214\u219213\u219212\u219210\u21927\u21923.<\/li>\n<\/ul>\n<p>Taking another example, if the linked lists are:<br \/>\n<strong>list 1:<\/strong> 1\u21922\u21923\u21929.<br \/>\n<strong>list 2:<\/strong> 4\u21925\u219211\u219217.<\/p>\n<ul>\n<li>In this case, when we merge list 1 and list 2, our merged list will be 1\u21922\u21923\u21924\u21925\u21929\u219211\u219217.<\/li>\n<li>And our output will be the reverse of this merged list, which is: 17\u219211\u21929\u21925\u21924\u21923\u21922\u21921.<\/li>\n<\/ul>\n<p><strong>Some more examples<\/strong><br \/>\nSample Input 1:<\/p>\n<ul>\n<li>list 1: 1\u21923\u21925\u21927<\/li>\n<li>list 2: 2\u21924\u21926\u21928<\/li>\n<\/ul>\n<p>Sample Output 1:<\/p>\n<ul>\n<li>8\u21927\u21926\u21925\u21924\u21923\u21922\u21921<\/li>\n<\/ul>\n<p>Sample Input 2:<\/p>\n<ul>\n<li>list 1: 2\u21925\u219210\u219215<\/li>\n<li>list 2: 3\u21926\u21927<\/li>\n<\/ul>\n<p>Sample Output 2:<\/p>\n<ul>\n<li>15\u219210\u21927\u21926\u21925\u21923\u21922<\/li>\n<\/ul>\n<p>Now, I think from the above examples, the problem statement is clear. Let\u2019s see how we can approach it.<br \/>\nBefore moving to the approach section, try to think how you can approach this problem.<\/p>\n<ul>\n<li>If stuck, no problem, we will thoroughly see how we can approach this problem in the next section.<\/li>\n<\/ul>\n<p>Well, the most naive idea is:<\/p>\n<ul>\n<li>Reverse the first list.<\/li>\n<li>Reverse the second list.<\/li>\n<li>And then merge two reversed linked lists.<br \/>\nUsing these above steps, we will get our required output.<\/li>\n<\/ul>\n<p>Another similar approach is first to merge both the list and then finally reverse the merged list.<br \/>\nBoth of the above approaches will work fine, but can we do this with some in-place algorithm and only one traversal using O(1) extra space?<\/p>\n<ul>\n<li>Let\u2019s see the approach for the same.<\/li>\n<\/ul>\n<p>Let\u2019s move to the approach section.<\/p>\n<h2>Approach To Merge 2 Sorted Linked List In Reverse Order<\/h2>\n<p>This approach is based on a merge style process.<\/p>\n<ul>\n<li>First, we initialize the resultant linked list as empty.<\/li>\n<li>And then, traverse both the linked lists from beginning to end and compare the current nodes of both the linked lists and insert the smaller of two at the beginning of the resultant linked list.<\/li>\n<li>Finally, our resultant linked list will have the merged list in reverse order.<\/li>\n<\/ul>\n<p>The following algorithm will explain the approach in detail.<\/p>\n<h2>Algorithm To Merge 2 Sorted Linked List In Reverse Order<\/h2>\n<ul>\n<li>\n<p>First, initialize the resultant linked list <strong>result<\/strong> as an empty list.<\/p>\n<\/li>\n<li>\n<p>Let <strong>h1<\/strong> and <strong>h2<\/strong> be the two pointers pointing to the head of <strong>list 1<\/strong> and <strong>list 2<\/strong>, respectively.<\/p>\n<\/li>\n<li>\n<p>Now we will traverse the two given linked lists <strong>while(h1!=NULL &amp;&amp; h2!=NULL)<\/strong>.<\/p>\n<ul>\n<li>We will find the smaller of two nodes pointed by <strong>h1<\/strong> and <strong>h2<\/strong>.<\/li>\n<li>After finding the smaller of the two nodes, we will insert the node with the smaller value at the front of the <strong>result<\/strong> list.<\/li>\n<li>Then we will move forward in the linked list with a smaller node value.<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>If either of the linked lists is traversed completely, then insert all the remaining nodes of another list into the <strong>result<\/strong> list.<\/p>\n<\/li>\n<li>\n<p>Finally, after all these steps, we will have our merged list in reverse order.<\/p>\n<\/li>\n<\/ul>\n<h3>Dry Run To Merge 2 Sorted Linked List In Reverse Order<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/p_1-11.png\" alt=\"\" \/><br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/p_2-12.png\" alt=\"\" \/><br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/p_3-6.png\" alt=\"\" \/><br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/p_4-2.png\" alt=\"\" \/><\/p>\n<h2>Code Implementation To Merge 2 Sorted Linked List In Reverse Order<\/h2>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_5090 {\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_5090 .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_5090 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_5090 .wpsm_nav-tabs > li.active > a, #tab_container_5090 .wpsm_nav-tabs > li.active > a:hover, #tab_container_5090 .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_5090 .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_5090 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_5090 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_5090 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_5090 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_5090 .wpsm_nav-tabs > li > a:hover , #tab_container_5090 .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_5090 .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_5090 .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_5090 .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_5090 .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_5090 .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_5090 .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_5090 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_5090 .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_5090 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_5090 .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_5090 .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_5090\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_5090\">\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_5090_1\" aria-controls=\"tabs_desc_5090_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_5090_2\" aria-controls=\"tabs_desc_5090_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_5090_3\" aria-controls=\"tabs_desc_5090_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_5090_4\" aria-controls=\"tabs_desc_5090_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_5090\">\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_5090_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#include &lt;stddef.h&gt;\r\n\r\nstruct Node\r\n{\r\n    int key;\r\n    struct Node* next;\r\n};\r\n \r\n\/\/ Given two non-empty linked lists 'a' and 'b'\r\nstruct Node* SortedMerge(struct Node *a,struct Node *b)\r\n{\r\n    \/\/ If both lists are empty\r\n    if (a==NULL &amp;&amp; b==NULL) return NULL;\r\n \r\n    \/\/ Initialize head of resultant list\r\n    struct Node *res = NULL;\r\n \r\n    \/\/ Traverse both lists while both of then\r\n    \/\/ have nodes.\r\n    while (a != NULL &amp;&amp; b != NULL)\r\n    {\r\n        \/\/ If a's current value is smaller or equal to\r\n        \/\/ b's current value.\r\n        if (a-&gt;key &lt;= b-&gt;key)\r\n        {\r\n            \/\/ Store next of current Node in first list\r\n           struct Node *temp = a-&gt;next;\r\n \r\n            \/\/ Add 'a' at the front of resultant list\r\n            a-&gt;next = res;\r\n            res = a;\r\n \r\n            \/\/ Move ahead in first list\r\n            a = temp;\r\n        }\r\n \r\n        \/\/ If a's value is greater. Below steps are similar\r\n        \/\/ to above (Only 'a' is replaced with 'b')\r\n        else\r\n        {\r\n            struct Node *temp = b-&gt;next;\r\n            b-&gt;next = res;\r\n            res = b;\r\n            b = temp;\r\n        }\r\n    }\r\n \r\n    \/\/ If second list reached end, but first list has\r\n    \/\/ nodes. Add remaining nodes of first list at the\r\n    \/\/ front of result list\r\n    while (a != NULL)\r\n    {\r\n        struct Node *temp = a-&gt;next;\r\n        a-&gt;next = res;\r\n        res = a;\r\n        a = temp;\r\n    }\r\n \r\n    \/\/ If first list reached end, but second list has\r\n    \/\/ node. Add remaining nodes of first list at the\r\n    \/\/ front of result list\r\n    while (b != NULL)\r\n    {\r\n        struct Node *temp = b-&gt;next;\r\n        b-&gt;next = res;\r\n        res = b;\r\n        b = temp;\r\n    }\r\n \r\n    return res;\r\n}\r\n \r\n\/* Function to print Nodes in a given linked list *\/\r\nvoid printList(struct Node *Node)\r\n{\r\n    while (Node!=NULL)\r\n    {\r\n        printf(&quot;%d &quot;,Node-&gt;key);\r\n        Node = Node-&gt;next;\r\n    }\r\n}\r\n \r\n\/* Utility function to create a new node with\r\n   given key *\/\r\nstruct Node *newNode(int key)\r\n{\r\n    struct Node* temp =\r\n           (struct Node*) malloc(sizeof(struct Node));\r\n    temp-&gt;key = key;\r\n    temp-&gt;next = NULL;\r\n    return temp;\r\n}\r\n \r\n\/* Driver program to test above functions*\/\r\nint main()\r\n{\r\n    \/* Start with the empty list *\/\r\n    struct Node* res = NULL;\r\n \r\n    \/* Let us create two sorted linked lists to test\r\n       the above functions. Created lists shall be\r\n         a: 5-&gt;10-&gt;15\r\n         b: 2-&gt;3-&gt;20  *\/\r\n    struct Node *a = newNode(5);\r\n    a-&gt;next = newNode(10);\r\n    a-&gt;next-&gt;next = newNode(15);\r\n \r\n    struct Node *b = newNode(2);\r\n    b-&gt;next = newNode(3);\r\n    b-&gt;next-&gt;next = newNode(20);\r\n \r\n    printf(&quot;List A before merge: &#92;n&quot;);\r\n    printList(a);\r\n \r\n    printf(&quot;&#92;nList B before merge: &#92;n&quot;);\r\n    printList(b);\r\n \r\n    \/* merge 2 increasing order LLs in decreasing order *\/\r\n    res = SortedMerge(a, b);\r\n \r\n    printf(&quot;&#92;nMerged Linked List is: &#92;n&quot;);\r\n    printList(res);\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_5090_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;iostream&gt;\r\nusing namespace std;\r\n\r\n\/* Node structure of linked list node *\/\r\nstruct Node\r\n{\r\n    int data;\r\n    struct Node* next;\r\n};\r\n\r\n\/* Using this function we will be merging both the linked list in such a way that merged list will be in reverse order *\/\r\nNode* MergeSortedReverse(Node *h1, Node *h2)\r\n{\r\n    if (h1==NULL &amp;&amp; h2==NULL) return NULL;\r\n    Node *result = NULL;\r\n    while (h1 != NULL &amp;&amp; h2 != NULL)\r\n    {\r\n        if (h1-&gt;data &lt;= h2-&gt;data)\r\n        {\r\n            Node *temp = h1-&gt;next;\r\n            h1-&gt;next = result;\r\n            result = h1;\r\n            h1 = temp;\r\n        }\r\n        else\r\n        {\r\n            Node *temp = h2-&gt;next;\r\n            h2-&gt;next = result;\r\n            result = h2;\r\n            h2 = temp;\r\n        }\r\n    }\r\n    while (h1 != NULL)\r\n    {\r\n        Node *temp = h1-&gt;next;\r\n        h1-&gt;next = result;\r\n        result = h1;\r\n        h1 = temp;\r\n    }\r\n    while (h2 != NULL)\r\n    {\r\n        Node *temp = h2-&gt;next;\r\n        h2-&gt;next = result;\r\n        result = h2;\r\n        h2 = temp;\r\n    }\r\n\r\n    return result;\r\n}\r\n\r\n\/* Using this function we will be printing the linked list content *\/\r\nvoid PrintingList(struct Node *Node)\r\n{\r\n    while (Node!=NULL)\r\n    {\r\n        cout&lt;&lt;Node-&gt;data&lt;&lt;&quot; -&gt; &quot;;\r\n        Node = Node-&gt;next;\r\n    }\r\n    cout&lt;&lt;&quot;NULL&quot;&lt;&lt;endl;\r\n}\r\n\r\n\/* Using this function we will creating a new list node *\/\r\nNode *newNode(int data)\r\n{\r\n    Node *temp = new Node;\r\n    temp-&gt;data = data;\r\n    temp-&gt;next = NULL;\r\n    return temp;\r\n}\r\n\r\nint main()\r\n{    struct Node* result = NULL;\r\n    Node *list1 = newNode(3);\r\n    list1-&gt;next = newNode(7);\r\n    list1-&gt;next-&gt;next = newNode(10);\r\n    list1-&gt;next-&gt;next-&gt;next = newNode(14);\r\n\r\n    Node *list2 = newNode(12);\r\n    list2-&gt;next = newNode(13);\r\n    list2-&gt;next-&gt;next = newNode(15);\r\n    cout&lt;&lt;&quot;Original linked list 1: &quot;&lt;&lt;endl;\r\n    PrintingList(list1);\r\n    cout&lt;&lt;&quot;Original linked list 2: &quot;&lt;&lt;endl;\r\n    PrintingList(list2);\r\n    result = MergeSortedReverse(list1, list2);\r\n    cout&lt;&lt;&quot;Merged list in reverse order: &quot;&lt;&lt;endl;\r\n    PrintingList(result);\r\n    return 0;\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_5090_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 ReverseMerge \r\n{\r\n\tNode head; \r\n\tNode a;\r\n    Node b;\r\n\r\n\t\/* Node Class *\/\r\n\tstatic class Node \r\n    {\r\n        int data;\r\n\t\tNode next;\r\n\r\n\t\tNode(int d) \r\n        {\r\n\t\t\tdata = d;\r\n\t\t\tnext = null;\r\n\t\t}\r\n\t}\r\n\tvoid printlist(Node node) {\r\n\t\twhile (node != null) {\r\n\t\t\tSystem.out.print(node.data + &quot; &quot;);\r\n\t\t\tnode = node.next;\r\n\t\t}\r\n\t}\r\n\tNode sortedmerge(Node node1, Node node2) {\r\n\t\t\r\n\t\t\/\/ if both the nodes are null\r\n\t\tif (node1 == null &amp;&amp; node2 == null) {\r\n\t\t\treturn null;\r\n\t\t}\r\n\t\t\/\/ resultant node\r\n\t\tNode res = null;\r\n\r\n\t\t\/\/ if both of them have nodes present traverse them\r\n\t\twhile (node1 != null &amp;&amp; node2 != null) {\r\n\r\n\t\t\t\/\/ Now compare both nodes current data\r\n\t\t\tif (node1.data &lt;= node2.data) {\r\n\t\t\t\tNode temp = node1.next;\r\n\t\t\t\tnode1.next = res;\r\n\t\t\t\tres = node1;\r\n\t\t\t\tnode1 = temp;\r\n\t\t\t} else {\r\n\t\t\t\tNode temp = node2.next;\r\n\t\t\t\tnode2.next = res;\r\n\t\t\t\tres = node2;\r\n\t\t\t\tnode2 = temp;\r\n\t\t\t}\r\n\t\t}\r\n\t\twhile (node1 != null) {\r\n\t\t\tNode temp = node1.next;\r\n\t\t\tnode1.next = res;\r\n\t\t\tres = node1;\r\n\t\t\tnode1 = temp;\r\n\t\t}\r\n\t\twhile (node2 != null) {\r\n\t\t\tNode temp = node2.next;\r\n\t\t\tnode2.next = res;\r\n\t\t\tres = node2;\r\n\t\t\tnode2 = temp;\r\n\t\t}\r\n\r\n\t\treturn res;\r\n\r\n\t}\r\n\r\n\tpublic static void main(String[] args) {\r\n\r\n\t\tReverseMerge list = new ReverseMerge();\r\n\t\tNode result = null;\r\n\r\n\t\tlist.a = new Node(5);\r\n\t\tlist.a.next = new Node(10);\r\n\t\tlist.a.next.next = new Node(15);\r\n\r\n\t\tlist.b = new Node(2);\r\n\t\tlist.b.next = new Node(3);\r\n\t\tlist.b.next.next = new Node(20);\r\n\r\n\t\tSystem.out.println(&quot;List a before merge :&quot;);\r\n\t\tlist.printlist(list.a);\r\n\t\tSystem.out.println(&quot;&quot;);\r\n\t\tSystem.out.println(&quot;List b before merge :&quot;);\r\n\t\tlist.printlist(list.b);\r\n\r\n\t\t\/\/ merge two sorted linkedlist in decreasing order\r\n\t\tresult = list.sortedmerge(list.a, list.b);\r\n\t\tSystem.out.println(&quot;&quot;);\r\n\t\tSystem.out.println(&quot;Merged linked list : &quot;);\r\n\t\tlist.printlist(result);\r\n\r\n\t}\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_5090_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# Node of a linked list\r\nclass Node:\r\n\tdef __init__(self, next = None, data = None):\r\n\t\tself.next = next\r\n\t\tself.data = data\r\n\r\ndef SortedMerge(a,b):\r\n\r\n\tif (a == None and b == None):\r\n\t\treturn None\r\n\r\n\tres = None\r\n\r\n\twhile (a != None and b != None):\r\n\t\r\n\t\tif (a.key &lt;= b.key):\r\n\r\n\t\t\ttemp = a.next\r\n\t\t\ta.next = res\r\n\t\t\tres = a\r\n\t\t\ta = temp\r\n\r\n\t\telse:\r\n\t\t\r\n\t\t\ttemp = b.next\r\n\t\t\tb.next = res\r\n\t\t\tres = b\r\n\t\t\tb = temp\r\n\t\t\r\n\twhile (a != None):\r\n\t\r\n\t\ttemp = a.next\r\n\t\ta.next = res\r\n\t\tres = a\r\n\t\ta = temp\r\n\t\r\n\twhile (b != None):\r\n\t\r\n\t\ttemp = b.next\r\n\t\tb.next = res\r\n\t\tres = b\r\n\t\tb = temp\r\n\t\r\n\treturn res\r\n\r\ndef printList(Node):\r\n\r\n\twhile (Node != None):\r\n\t\r\n\t\tprint( Node.key, end = &quot; &quot;)\r\n\t\tNode = Node.next\r\n\t\r\ndef newNode( key):\r\n\r\n\ttemp = Node()\r\n\ttemp.key = key\r\n\ttemp.next = None\r\n\treturn temp\r\n\r\nres = None\r\na = newNode(3)\r\na.next = newNode(7)\r\na.next.next = newNode(10)\r\na.next.next.next = newNode(14)\r\n\r\nb = newNode(12)\r\nb.next = newNode(13)\r\nb.next.next = newNode(15)\r\n\r\nprint( &quot;Original linked list 1:&quot;)\r\nprintList(a)\r\n\r\nprint( &quot;&#92;nOriginal linked list 2:&quot;)\r\nprintList(b)\r\n\r\nres = SortedMerge(a, b)\r\n\r\nprint(&quot;&#92;nMerged list in reverse order:&quot;)\r\nprintList(res)\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_5090 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_5090 a\"),jQuery(\"#tab-content_5090\"));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<pre><code>Output\nOriginal linked list 1:\n3 -&gt; 7 -&gt; 10 -&gt; 14 -&gt; NULL\nOriginal linked list 2:\n12 -&gt; 13 -&gt; 15 -&gt; NULL\nMerged list in reverse order:\n15 -&gt; 14 -&gt; 13 -&gt; 12 -&gt; 10 -&gt; 7 -&gt; 3 -&gt; NULL<\/code><\/pre>\n<p><strong>Time Complexity To Merge 2 Sorted Linked List In Reverse Order:<\/strong> O(max(m,n)), where m, n are the size of Linked Lists.<\/p>\n<p><strong>Conclusion<\/strong><br \/>\nIn conclusion, merging two sorted linked lists in reverse order is a skillful maneuver that combines the principles of sorting and linked list manipulation. Through careful consideration of the elements in the linked lists, the merging process unfolds in a way that creates a new list with the desired reversed order. This not only requires attention to the logical flow of the data but also an understanding of pointers and node manipulation. As you embark on your journey of coding and problem-solving, the ability to merge and manipulate linked lists in reverse order will undoubtedly be a valuable addition to your toolkit.<\/p>\n<h2>FAQ related to Merge two sorted linked lists such that the merged list is in reverse order<\/h2>\n<p>Here are some of the FAQs related to Merge two sorted linked lists such that the merged list is in reverse order<\/p>\n<p><strong>Q1: Why would I need to merge two sorted linked lists in reverse order?<br \/>\nA1:<\/strong> Merging two sorted linked lists in reverse order can be useful in scenarios where you need the final merged list to be presented in descending order. This might be required for specific algorithms or data processing tasks.<\/p>\n<p><strong>Q2: What is the key principle behind merging two sorted linked lists in reverse order?<br \/>\nA2:<\/strong> The key principle involves systematically merging the two lists while ensuring that the resulting merged list is constructed in reverse order. This typically requires careful manipulation of pointers and nodes to achieve the desired outcome.<\/p>\n<p><strong>Q3: Are there specific algorithms or methods for merging linked lists in reverse order?<br \/>\nA3:<\/strong> Yes, there are various algorithms and approaches for merging two sorted linked lists in reverse order. One common method involves iterating through both lists, comparing elements, and appropriately linking nodes to form the merged list in reverse.<\/p>\n<p><strong>Q4: Can the same principles be applied to merge more than two sorted linked lists in reverse order?<br \/>\nA4:<\/strong> Yes, the principles of merging sorted linked lists in reverse order can be extended to merge more than two lists. The key is to apply the merging logic iteratively, considering each additional list in the process.<\/p>\n<p><strong>Q5: How does the time complexity of merging two sorted linked lists in reverse order compare to merging them in ascending order?<br \/>\nA5:<\/strong> The time complexity of merging two sorted linked lists in reverse order is generally similar to merging them in ascending order. However, the specific implementation details may introduce slight variations, and the choice of algorithm can impact overall performance.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome to the realm of linked list manipulation! If you&#8217;ve ever wondered how to efficiently merge two sorted linked lists in such a way that the resulting list is in reverse order, you&#8217;re about to embark on a fascinating journey. Merging linked lists is a common task in programming, but when the requirement is 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-5089","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>Merge two sorted linked lists such that the merged list is in reverse order | Linked list articles | PrepBytes Blog<\/title>\n<meta name=\"description\" content=\"Learn how to merge two sorted linked lists in such a way that the merged list is in reverse order.\" \/>\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\/merge-two-sorted-linked-lists-such-that-the-merged-list-is-in-reverse-order\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Merge two sorted linked lists such that the merged list is in reverse order | Linked list articles | PrepBytes Blog\" \/>\n<meta property=\"og:description\" content=\"Learn how to merge two sorted linked lists in such a way that the merged list is in reverse order.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/merge-two-sorted-linked-lists-such-that-the-merged-list-is-in-reverse-order\/\" \/>\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-20T07:42:34+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-28T04:56:59+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645007374578-Article_155.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\/merge-two-sorted-linked-lists-such-that-the-merged-list-is-in-reverse-order\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-two-sorted-linked-lists-such-that-the-merged-list-is-in-reverse-order\/\"},\"author\":{\"name\":\"PrepBytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e\"},\"headline\":\"Merge two sorted linked lists such that the merged list is in reverse order\",\"datePublished\":\"2021-09-20T07:42:34+00:00\",\"dateModified\":\"2023-11-28T04:56:59+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-two-sorted-linked-lists-such-that-the-merged-list-is-in-reverse-order\/\"},\"wordCount\":1113,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-two-sorted-linked-lists-such-that-the-merged-list-is-in-reverse-order\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645007374578-Article_155.png\",\"articleSection\":[\"Linked list articles\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/merge-two-sorted-linked-lists-such-that-the-merged-list-is-in-reverse-order\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-two-sorted-linked-lists-such-that-the-merged-list-is-in-reverse-order\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/merge-two-sorted-linked-lists-such-that-the-merged-list-is-in-reverse-order\/\",\"name\":\"Merge two sorted linked lists such that the merged list is in reverse order | Linked list articles | PrepBytes Blog\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-two-sorted-linked-lists-such-that-the-merged-list-is-in-reverse-order\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-two-sorted-linked-lists-such-that-the-merged-list-is-in-reverse-order\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645007374578-Article_155.png\",\"datePublished\":\"2021-09-20T07:42:34+00:00\",\"dateModified\":\"2023-11-28T04:56:59+00:00\",\"description\":\"Learn how to merge two sorted linked lists in such a way that the merged list is in reverse order.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-two-sorted-linked-lists-such-that-the-merged-list-is-in-reverse-order\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/merge-two-sorted-linked-lists-such-that-the-merged-list-is-in-reverse-order\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-two-sorted-linked-lists-such-that-the-merged-list-is-in-reverse-order\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645007374578-Article_155.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645007374578-Article_155.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-two-sorted-linked-lists-such-that-the-merged-list-is-in-reverse-order\/#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\":\"Merge two sorted linked lists such that the merged list is in reverse order\"}]},{\"@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":"Merge two sorted linked lists such that the merged list is in reverse order | Linked list articles | PrepBytes Blog","description":"Learn how to merge two sorted linked lists in such a way that the merged list is in reverse order.","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\/merge-two-sorted-linked-lists-such-that-the-merged-list-is-in-reverse-order\/","og_locale":"en_US","og_type":"article","og_title":"Merge two sorted linked lists such that the merged list is in reverse order | Linked list articles | PrepBytes Blog","og_description":"Learn how to merge two sorted linked lists in such a way that the merged list is in reverse order.","og_url":"https:\/\/prepbytes.com\/blog\/merge-two-sorted-linked-lists-such-that-the-merged-list-is-in-reverse-order\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2021-09-20T07:42:34+00:00","article_modified_time":"2023-11-28T04:56:59+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645007374578-Article_155.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\/merge-two-sorted-linked-lists-such-that-the-merged-list-is-in-reverse-order\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/merge-two-sorted-linked-lists-such-that-the-merged-list-is-in-reverse-order\/"},"author":{"name":"PrepBytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e"},"headline":"Merge two sorted linked lists such that the merged list is in reverse order","datePublished":"2021-09-20T07:42:34+00:00","dateModified":"2023-11-28T04:56:59+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/merge-two-sorted-linked-lists-such-that-the-merged-list-is-in-reverse-order\/"},"wordCount":1113,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/merge-two-sorted-linked-lists-such-that-the-merged-list-is-in-reverse-order\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645007374578-Article_155.png","articleSection":["Linked list articles"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/merge-two-sorted-linked-lists-such-that-the-merged-list-is-in-reverse-order\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/merge-two-sorted-linked-lists-such-that-the-merged-list-is-in-reverse-order\/","url":"https:\/\/prepbytes.com\/blog\/merge-two-sorted-linked-lists-such-that-the-merged-list-is-in-reverse-order\/","name":"Merge two sorted linked lists such that the merged list is in reverse order | Linked list articles | PrepBytes Blog","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/merge-two-sorted-linked-lists-such-that-the-merged-list-is-in-reverse-order\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/merge-two-sorted-linked-lists-such-that-the-merged-list-is-in-reverse-order\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645007374578-Article_155.png","datePublished":"2021-09-20T07:42:34+00:00","dateModified":"2023-11-28T04:56:59+00:00","description":"Learn how to merge two sorted linked lists in such a way that the merged list is in reverse order.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/merge-two-sorted-linked-lists-such-that-the-merged-list-is-in-reverse-order\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/merge-two-sorted-linked-lists-such-that-the-merged-list-is-in-reverse-order\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/merge-two-sorted-linked-lists-such-that-the-merged-list-is-in-reverse-order\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645007374578-Article_155.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645007374578-Article_155.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/merge-two-sorted-linked-lists-such-that-the-merged-list-is-in-reverse-order\/#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":"Merge two sorted linked lists such that the merged list is in reverse order"}]},{"@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\/5089","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=5089"}],"version-history":[{"count":8,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/5089\/revisions"}],"predecessor-version":[{"id":18398,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/5089\/revisions\/18398"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=5089"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=5089"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=5089"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}