{"id":5896,"date":"2021-11-08T11:22:45","date_gmt":"2021-11-08T11:22:45","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=5896"},"modified":"2022-11-28T11:11:01","modified_gmt":"2022-11-28T11:11:01","slug":"merge-k-sorted-doubly-linked-list-in-sorted-order","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/merge-k-sorted-doubly-linked-list-in-sorted-order\/","title":{"rendered":"Merge K Sorted Doubly Linked List in Sorted Order"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012425288-Article_211.png\" alt=\"\" \/><\/p>\n<p>A doubly linked list is a type of linked list in which the node consists of three parts i.e. the data, the pointer of the previous node and the pointer of the next node.<br \/>\nIn this article we will study how to sort k sorted doubly linked list. Let us have a look to the problem statement for a better understanding of how to sort k sorted doubly linked list.<\/p>\n<h2>How to sort k sorted doubly linked list <\/h2>\n<p>In this problem, we will be given <strong>K<\/strong> sorted doubly linked lists. We need to merge all lists such that the newly created doubly linked list is also in sorted order.<\/p>\n<p>If you want to see how to merge <strong>K<\/strong> sorted singly-linked lists, check out these articles <a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/merge-k-sorted-linked-lists-set-1\/\">merge K sorted linked lists(set 1)<\/a> and <a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/merge-k-sorted-linked-lists-set-2-using-min-heap\/\">merge K sorted linked lists(set 2 &#8211; using Min heap)<\/a>.<\/p>\n<p>The problem statement is quite straightforward, we will be given <strong>K<\/strong> doubly-linked lists that are sorted in nature, and then we need to form a doubly-linked list using the nodes of all the given linked lists such that the newly formed list is sorted in order. <\/p>\n<p>Let&#8217;s try to understand the problem with the help of an example:<br \/>\nIf the sorted doubly-linked lists given to us are:<br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/10\/p_1-10.jpg\" alt=\"\" \/><\/p>\n<ul>\n<li>Then, according to the problem statement, we need to merge all these given linked lists into a single doubly linked list in such a way that after merging, the final list is also sorted in nature.<\/li>\n<li>The list that we need to return must contain all the nodes of all three given lists in sorted order.<\/li>\n<li>So, after merging, the newly formed sorted list will be:<br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/10\/p_2-10.jpg\" alt=\"\" \/><\/li>\n<\/ul>\n<p>At this point, we have understood the problem statement. Now we will try to formulate an approach for this problem.<\/p>\n<p>Before moving to the approach section, try to think about 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>Let\u2019s move to the approach section.<\/p>\n<h2>Approach 1 of how to sort k sorted doubly linked list <\/h2>\n<ul>\n<li>Initialize a <strong>result list<\/strong> with the <strong>first list<\/strong>.<\/li>\n<li>Traverse all <strong>K<\/strong> lists one by one, starting from the second list, and merge them with the initialized result list using the <strong>merge two sorted list<\/strong> concepts.<\/li>\n<li>At last, when we have traversed all the <strong>K<\/strong> lists, our result list will be containing all the nodes of the <strong>K<\/strong> lists in sorted order.<\/li>\n<li>Finally, we can return this <strong>result list<\/strong> as our output.<\/li>\n<\/ul>\n<p>To see how to merge two sorted linked lists, check out this <a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/merge-two-sorted-linked-lists\/\">article<\/a>.<\/p>\n<p><strong>Some Important Observations<\/strong><\/p>\n<ul>\n<li>Every time we add a sorted list into our result list, its size increases by <strong>n<\/strong>.<\/li>\n<li>So, when we add the second list, the complexity is <strong>2n<\/strong>. Then, on adding the third list, the complexity is <strong>3n<\/strong>. Similarly, when we are adding the K<sup>th<\/sup>  list, the complexity is *<em>K<\/em>n**.\n<ul>\n<li>So, the total complexity is <strong>(2n + 3n + 4n + \u2026.. + K<em>n) = n <\/em> ( 2+3+4+&#8230;+K)<\/strong>, which is asymptotically equal to (n*K<sup>2<\/sup> ).<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><strong>Time Complexity:<\/strong> O(n* K<sup>2<\/sup> ), <strong>n<\/strong> is the number of nodes in the list, and <strong>K<\/strong> is the total number of lists<\/p>\n<p><strong>Space Complexity:<\/strong> O(1)<\/p>\n<p>We can observe that if the number of lists given is quite large, then the above approach will result in TLE. So, we need to reduce the time complexity, which is possible if we use the divide and conquer technique.<\/p>\n<h2>Approach 2 of how to sort k sorted doubly linked list <\/h2>\n<ul>\n<li>In this approach, we merge the linked lists in pairs.\n<ul>\n<li>In the first iteration, we will have <strong>K\/2<\/strong> pairs so; we will merge them using the merge two sorted lists concept.<\/li>\n<li>Then after the first iteration, we will have <strong>K\/2<\/strong> lists, i.e., <strong>K\/4<\/strong> pairs of lists to merge.<\/li>\n<li>After each iteration, the number of lists to be merged will reduce by half of their previous count.<\/li>\n<li>At last, we will be left with a single list which will contain all lists merged in sorted order.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>## Algorithm of how to sort k sorted doubly linked list<\/p>\n<p>1. Initialize a variable <strong>back<\/strong> with the last index of the array containing the head of all the lists.<br \/>\n2. Run a while loop till <strong>back<\/strong> is not equal to zero.<br \/>\n    &#8211; Inside while loop, we need to merge pairs so, initialize two variables, i.e., <strong>i<\/strong> with 0 and <strong>j<\/strong> with <strong>back<\/strong>.<br \/>\n    &#8211; Now, we need to run a while loop as long as <strong>i<\/strong> is less than <strong>j<\/strong>.<br \/>\n    &#8211; Now merge i<sup>th<\/sup> and j<sup>th<\/sup> list and store it as an i<sup>th<\/sup>  element of the array.<br \/>\n    &#8211; Now, increment <strong>i<\/strong> by one and decrement <strong>j<\/strong> by one.<br \/>\n    &#8211; If <strong>i<\/strong> is greater than or equal to <strong>j<\/strong>, we need to update <strong>back<\/strong> by <strong>j<\/strong>.<br \/>\n3. After execution of both the while loops, we need to return the first element of the array.<br \/>\n4. In case you are not aware of how to merge two sorted linked lists, please check this <a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/merge-two-sorted-linked-lists\/\">article<\/a>.<\/p>\n<h3>Dry Run of how to sort k sorted doubly linked list<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/10\/p_3-8.jpg\" alt=\"\" \/><br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/10\/p_4-6.jpg\" alt=\"\" \/><\/p>\n<p>## Code Implementation of how to sort k sorted doubly linked list<\/p>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_5897 {\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_5897 .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_5897 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_5897 .wpsm_nav-tabs > li.active > a, #tab_container_5897 .wpsm_nav-tabs > li.active > a:hover, #tab_container_5897 .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_5897 .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_5897 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_5897 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_5897 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_5897 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_5897 .wpsm_nav-tabs > li > a:hover , #tab_container_5897 .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_5897 .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_5897 .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_5897 .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_5897 .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_5897 .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_5897 .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_5897 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_5897 .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_5897 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_5897 .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_5897 .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_5897\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_5897\">\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_5897_1\" aria-controls=\"tabs_desc_5897_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\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_5897\">\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_5897_1\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"cpp\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include<bits\/stdc++.h>\r\nusing namespace std;\r\n\r\n\/\/ class with constructor for a node of Linked list\r\nclass Node {\r\n    public:\r\n    int data;\r\n    Node* next,*prev;\r\n    \/\/ constructor\r\n    Node(int x){\r\n        data = x;\r\n        next = NULL;\r\n        prev = NULL;\r\n    }\r\n};\r\n\r\nvoid add_at_begin(Node** head, int x)\r\n{\r\n \r\n    Node* new_node = new Node(x);\/\/create node of doubly linked \r\n   \/\/list\r\n \r\n    \/\/assign the data entered by user\r\n    new_node->data = x;\r\n \r\n    \/\/ node is inserted in beginning so, prev will always \r\n    \/\/ be NULL\r\n    new_node->prev = NULL;\r\n \r\n    \/\/the node created is inserted at beginning so we need to\r\n    \/\/connect it with previous head\r\n    new_node->next = (*head);\r\n     \r\n    \/\/ change the head to the current node\u2019s address\r\n    if ((*head) != NULL)\r\n        (*head)->prev = new_node;\r\n \r\n    (*head) = new_node;\r\n}\r\n\r\n\r\n\/\/ This function prints data of the linked list\r\nvoid print(Node* head)\r\n{\r\n    Node* curr = head;\r\n    while (curr != NULL) {\r\n        cout << curr->data << \" , \";\r\n        curr = curr->next;\r\n    }\r\n    cout<<\"NULL\"<<endl;\r\n}\r\n\r\n\/\/ This function merges two sorted linked lists into one sorted\r\n\/\/ list\r\nNode *mergeTwoLists(Node *l1, Node *l2) {\r\n    Node dummy(INT_MIN);\r\n    Node *tail = &dummy;\r\n        \r\n    while (l1 && l2) {\r\n        if (l1->data < l2->data) {\r\n            tail->next = l1;\r\n            l1->prev = tail;\r\n            l1 = l1->next;\r\n        } else {\r\n            tail->next = l2;\r\n            l2->prev = tail;\r\n            l2 = l2->next;\r\n        }\r\n        tail = tail->next;\r\n    }\r\n\r\n    \/\/tail->next = l1 ? l1 : l2;\r\n    if(l1 != NULL){\r\n        tail->next = l1;\r\n        l1->prev = tail;\r\n    }else{\r\n        tail->next = l2;\r\n        l2->prev = tail;\r\n    }\r\n    return dummy.next;\r\n}\r\n\r\n\r\n\/\/ This function merges 'K' sorted lists into one sorted list\r\nNode* mergeKLists(Node* arr[], int back)\r\n{\r\n    \/\/ run the while loop till 'back' is not equal to\r\n    while (back != 0) {\r\n        int i = 0, j = back;\r\n \r\n        while (i < j) {\r\n            \/\/ merge ith and jth list and store the list at\r\n            \/\/ ith index of array\r\n            arr[i] = mergeTwoLists(arr[i], arr[j]);\r\n \r\n            \/\/ increment 'i' and decrement 'j' by one\r\n            i++, j--;\r\n \r\n            \/\/ update 'back' by 'j' once j is less than or\r\n            \/\/ equal to 'i'\r\n            if (i >= j)\r\n                back = j;\r\n        }\r\n    }\r\n \r\n    return arr[0];\r\n}\r\n\r\n\/\/ main function\r\nint main()\r\n{\r\n    Node * arr[3];\r\n\r\n    Node* h1 = NULL;\r\n    add_at_begin(&h1, 5);\r\n    add_at_begin(&h1, 3);\r\n    add_at_begin(&h1, 2);\r\n    cout<<\"First linked list: \";\r\n    print(h1);\r\n\r\n    Node* h2 = NULL;\r\n    add_at_begin(&h2, 8);\r\n    add_at_begin(&h2, 4);\r\n    add_at_begin(&h2, 1);\r\n    cout<<\"Second linked list: \";\r\n    print(h2);\r\n\r\n    Node* h3 = NULL;\r\n    add_at_begin(&h3, 9);\r\n    add_at_begin(&h3, 7);\r\n    add_at_begin(&h3, 6);\r\n    cout<<\"Third linked list: \";\r\n    print(h3);\r\n\r\n    arr[0] = h1;\r\n    arr[1] = h2;\r\n    arr[2] = h3;\r\n    \r\n    cout<<\"Final merged linked list: \";\r\n    print(mergeKLists(arr,2));\r\n    return 0;\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\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_5897 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_5897 a\"),jQuery(\"#tab-content_5897\"));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<p>    Output<br \/>\n    First linked list: 2 , 3 , 5 , NULL<br \/>\n    Second linked list: 1 , 4 , 8 , NULL<br \/>\n    Third linked list: 6 , 7 , 9 , NULL<br \/>\n    Final merged linked list: 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , NULL<\/p>\n<p><strong>Time Complexity of how to sort k sorted doubly linked list :<\/strong> O( n<em> K <\/em> log(K) ), <strong>n<\/strong> is the number of nodes in the list and <strong>K<\/strong> is the total number of lists.<\/p>\n<p>**Conclusion**<\/p>\n<p>This blog taught the proper explanation and solution of how to sort k sorted doubly linked list in the most optimal way. We hope we will clear all your doubts regarding the problem and enhance your knowledge. Also, our experts have created some famous and interesting problems over the linked list. you can follow this link <a href=\"https:\/\/mycode.prepbytes.com\/interview-coding\/practice\/linked-list\">Linked List<\/a>.<\/p>\n<p>## FAQs regarding how to sort k sorted doubly linked list<\/p>\n<p>**1. Are doubly linked lists sorted?**<br \/>\nGiven a sorted doubly linked list and a value to insert, write a function to insert the value in a sorted way. Algorithm: Let input doubly linked list is sorted in increasing order. The new node passed to the function contains data in the data part and the previous and next links are set to NULL.<\/p>\n<p>**2. Is doubly linked follows FIFO?**<br \/>\nIn doubly or two-way linked lists, two pointers are used in the structure, where one pointer points in the forward direction and the other points in the backward direction. These two pointers allow us to traverse a linked list in both ways, that is, in First in First Out (FIFO) order as well as LIFO order.<\/p>\n<p>**3. How do you sort a doubly linked list in C++?**<br \/>\nWe can sort a doubly linked list as:<br \/>\n&#8211; Define a node current that will point to the head.<br \/>\n&#8211; Define another node index that will point to the node next to the current.<br \/>\n&#8211; Compare data of current and index nodes.<br \/>\n&#8211; Current will point to current.<br \/>\n&#8211; Continue this process till the entire list is sorted.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A doubly linked list is a type of linked list in which the node consists of three parts i.e. the data, the pointer of the previous node and the pointer of the next node. In this article we will study how to sort k sorted doubly linked list. Let us have a look to the [&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":[125],"tags":[],"class_list":["post-5896","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 K Sorted Doubly Linked List in Sorted Order | Linked list articles | PrepBytes Blog<\/title>\n<meta name=\"description\" content=\"Learn how to merge **K** sorted doubly linked lists in sorted 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-k-sorted-doubly-linked-list-in-sorted-order\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Merge K Sorted Doubly Linked List in Sorted Order | Linked list articles | PrepBytes Blog\" \/>\n<meta property=\"og:description\" content=\"Learn how to merge **K** sorted doubly linked lists in sorted order.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/merge-k-sorted-doubly-linked-list-in-sorted-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-11-08T11:22:45+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-28T11:11:01+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012425288-Article_211.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=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-k-sorted-doubly-linked-list-in-sorted-order\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-k-sorted-doubly-linked-list-in-sorted-order\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Merge K Sorted Doubly Linked List in Sorted Order\",\"datePublished\":\"2021-11-08T11:22:45+00:00\",\"dateModified\":\"2022-11-28T11:11:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-k-sorted-doubly-linked-list-in-sorted-order\/\"},\"wordCount\":1164,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-k-sorted-doubly-linked-list-in-sorted-order\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012425288-Article_211.png\",\"articleSection\":[\"Linked list articles\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/merge-k-sorted-doubly-linked-list-in-sorted-order\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-k-sorted-doubly-linked-list-in-sorted-order\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/merge-k-sorted-doubly-linked-list-in-sorted-order\/\",\"name\":\"Merge K Sorted Doubly Linked List in Sorted Order | Linked list articles | PrepBytes Blog\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-k-sorted-doubly-linked-list-in-sorted-order\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-k-sorted-doubly-linked-list-in-sorted-order\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012425288-Article_211.png\",\"datePublished\":\"2021-11-08T11:22:45+00:00\",\"dateModified\":\"2022-11-28T11:11:01+00:00\",\"description\":\"Learn how to merge **K** sorted doubly linked lists in sorted order.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-k-sorted-doubly-linked-list-in-sorted-order\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/merge-k-sorted-doubly-linked-list-in-sorted-order\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-k-sorted-doubly-linked-list-in-sorted-order\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012425288-Article_211.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012425288-Article_211.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-k-sorted-doubly-linked-list-in-sorted-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 K Sorted Doubly Linked List in Sorted 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\/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":"Merge K Sorted Doubly Linked List in Sorted Order | Linked list articles | PrepBytes Blog","description":"Learn how to merge **K** sorted doubly linked lists in sorted 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-k-sorted-doubly-linked-list-in-sorted-order\/","og_locale":"en_US","og_type":"article","og_title":"Merge K Sorted Doubly Linked List in Sorted Order | Linked list articles | PrepBytes Blog","og_description":"Learn how to merge **K** sorted doubly linked lists in sorted order.","og_url":"https:\/\/prepbytes.com\/blog\/merge-k-sorted-doubly-linked-list-in-sorted-order\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2021-11-08T11:22:45+00:00","article_modified_time":"2022-11-28T11:11:01+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012425288-Article_211.png","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/merge-k-sorted-doubly-linked-list-in-sorted-order\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/merge-k-sorted-doubly-linked-list-in-sorted-order\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Merge K Sorted Doubly Linked List in Sorted Order","datePublished":"2021-11-08T11:22:45+00:00","dateModified":"2022-11-28T11:11:01+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/merge-k-sorted-doubly-linked-list-in-sorted-order\/"},"wordCount":1164,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/merge-k-sorted-doubly-linked-list-in-sorted-order\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012425288-Article_211.png","articleSection":["Linked list articles"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/merge-k-sorted-doubly-linked-list-in-sorted-order\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/merge-k-sorted-doubly-linked-list-in-sorted-order\/","url":"https:\/\/prepbytes.com\/blog\/merge-k-sorted-doubly-linked-list-in-sorted-order\/","name":"Merge K Sorted Doubly Linked List in Sorted Order | Linked list articles | PrepBytes Blog","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/merge-k-sorted-doubly-linked-list-in-sorted-order\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/merge-k-sorted-doubly-linked-list-in-sorted-order\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012425288-Article_211.png","datePublished":"2021-11-08T11:22:45+00:00","dateModified":"2022-11-28T11:11:01+00:00","description":"Learn how to merge **K** sorted doubly linked lists in sorted order.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/merge-k-sorted-doubly-linked-list-in-sorted-order\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/merge-k-sorted-doubly-linked-list-in-sorted-order\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/merge-k-sorted-doubly-linked-list-in-sorted-order\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012425288-Article_211.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012425288-Article_211.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/merge-k-sorted-doubly-linked-list-in-sorted-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 K Sorted Doubly Linked List in Sorted 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\/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\/5896","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=5896"}],"version-history":[{"count":5,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/5896\/revisions"}],"predecessor-version":[{"id":10794,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/5896\/revisions\/10794"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=5896"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=5896"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=5896"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}