{"id":5127,"date":"2021-09-21T07:51:27","date_gmt":"2021-09-21T07:51:27","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=5127"},"modified":"2022-06-15T11:12:24","modified_gmt":"2022-06-15T11:12:24","slug":"merge-k-sorted-linked-lists-set-1","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/merge-k-sorted-linked-lists-set-1\/","title":{"rendered":"Merge K sorted linked lists | Set 1"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1655291503614-1645007426664-Article_157.png\" alt=\"\" \/><\/p>\n<h3>Introduction<\/h3>\n<p>The linked list is one of the most important concepts to know while preparing for interviews. Having a good grasp of a linked list can be a huge plus point in coding interviews.<\/p>\n<\/p>\n<h3>Problem Statement<\/h3>\n<p>In this problem, we will be given <strong>K<\/strong> sorted linked lists. We need to merge all lists such that the newly created list is also in sorted order.<\/p>\n<h3>Problem Statement Understanding<\/h3>\n<p>The problem statement is quite straightforward, we will be given <strong>K<\/strong> linked lists that are sorted in nature, and then we need to form a 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 examples:<\/p>\n<p>If the sorted lists given to us are 2\u21923\u21925\u2192NULL, 1\u21924\u21928\u2192NULL, and 6\u21927\u21929\u2192NULL.<\/p>\n<ul>\n<li>According to the problem statement, we need to merge all these given linked lists into a single linked list in such a way that after merging, the final linked list is 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 1\u21922\u21923\u21924\u21925\u21926\u21927\u21928\u21929\u2192NULL<\/li>\n<\/ul>\n<p>Let\u2019s take another example:<br \/>\nIf the sorted lists given to us are 2\u21928\u21929\u219210\u2192NULL, 11\u219213\u219217\u2192NULL.<\/p>\n<ul>\n<li>In this case, after merging all the given lists, the final resultant sorted linked list will be 2\u21928\u21929\u219210\u219211\u219213\u219217\u2192NULL<\/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<h3>Approach 1<\/h3>\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 <strong>result list<\/strong> using the merge two sorted list 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><strong>Note:<\/strong> In case if you are not aware of how to merge two sorted linked lists, <a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/merge-two-sorted-linked-lists\/\">please check this article<\/a>.<\/p>\n<h4>Some Important Observations<\/h4>\n<ul>\n<li>Every time we add a sorted list into our result list, its size increases by <strong>N<\/strong> (Complexity wise in the worst case we will take <strong>N<\/strong> as the size of the longest list out of all <strong>K<\/strong> lists).<\/li>\n<li>So, when we add the second list, the complexity is <strong>2<em>N<strong>. Then, on adding the third list, the complexity is <\/strong>3<\/em>N<\/strong>.<\/li>\n<li>Similarly, when we are adding the K<sup>th<\/sup> list, the complexity is *<em>K<\/em>N**.<\/li>\n<li>So, the total complexity is <strong>(2<em>N + 3<\/em>N + 4<em>N + &#8230;. + K<\/em>N) = N <em> (2+3+4+&#8230;+K)<strong>, which is asymptotically equal to <\/strong>(N<\/em>K<sup>2<\/sup>)<\/strong>.<\/li>\n<\/ul>\n<p><strong>Time Complexity:<\/strong> *<em>(N<\/em>K<sup>2<\/sup>)<strong>, <\/strong>N<strong> is the number of nodes in the list, and <\/strong>K<strong> is the total number of lists<br \/>\n<\/strong>Space Complexity:** 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. <\/p>\n<ul>\n<li>So, we need to reduce the time complexity, which is possible if we use the divide and conquer technique.<\/li>\n<\/ul>\n<h3>Approach 2<\/h3>\n<ul>\n<li>In this approach, we merge the linked lists in pairs.<\/li>\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<p>Let&#8217;s move to the algorithm section.<\/p>\n<h3>Algorithm<\/h3>\n<ul>\n<li>Initialize a variable <strong>back<\/strong> with the last array index (this array is containing the head of all the lists).<\/li>\n<li>Run a while loop till <strong>back<\/strong> is not equal to 0.<\/li>\n<li>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>.<\/li>\n<li>Now, we need to run a while loop as long as <strong>i<\/strong> is less than <strong>j<\/strong>.<\/li>\n<li>Now merge i<sup>th<\/sup> and j<sup>th<\/sup> list and store it as i<sup>th<\/sup> element of the array.<\/li>\n<li>Now, increment <strong>i<\/strong> by one and decrement <strong>j<\/strong> by one.<\/li>\n<li>If <strong>i<\/strong> is greater than or equal to <strong>j<\/strong>, we need to update <strong>back<\/strong>, by <strong>j<\/strong>. <\/li>\n<li>After execution of both the while loops, we need to return the first element of the array.<\/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-16.png\" alt=\"\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/p_2-17.png\" alt=\"\" \/><\/p>\n<h3>Code Implementation<\/h3>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_8516 {\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_8516 .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_8516 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_8516 .wpsm_nav-tabs > li.active > a, #tab_container_8516 .wpsm_nav-tabs > li.active > a:hover, #tab_container_8516 .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_8516 .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_8516 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_8516 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_8516 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_8516 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_8516 .wpsm_nav-tabs > li > a:hover , #tab_container_8516 .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_8516 .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_8516 .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_8516 .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_8516 .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_8516 .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_8516 .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_8516 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_8516 .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_8516 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_8516 .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_8516 .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_8516\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_8516\">\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_8516_1\" aria-controls=\"tabs_desc_8516_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_8516_2\" aria-controls=\"tabs_desc_8516_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_8516_3\" aria-controls=\"tabs_desc_8516_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_8516_4\" aria-controls=\"tabs_desc_8516_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_8516\">\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_8516_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\r\n#include<stdio.h>\r\n#include<stdlib.h>\r\n#include<stdbool.h>\r\n\r\nstruct Node {\r\n    int data;\r\n    struct Node* next;\r\n};\r\n \r\n\/* Function to print nodes in\r\n   a given linked list *\/\r\nvoid printList(struct Node* node)\r\n{\r\n    while (node != NULL) {\r\n        printf(\"%d \", node->data);\r\n        node = node->next;\r\n    }\r\n}\r\n \r\n\/\/ The main function that\r\n\/\/ takes an array of lists\r\n\/\/ arr[0..last] and generates\r\n\/\/ the sorted output\r\nstruct Node* mergeKLists(struct Node* arr[], int last)\r\n{\r\n \r\n    \/\/ Traverse form second list to last\r\n    for (int i = 1; i <= last; i++) {\r\n        while (true) {\r\n            \/\/ head of both the lists,\r\n            \/\/ 0 and ith list.\r\n            struct Node *head_0 = arr[0], *head_i = arr[i];\r\n \r\n            \/\/ Break if list ended\r\n            if (head_i == NULL)\r\n                break;\r\n \r\n            \/\/ Smaller than first element\r\n            if (head_0->data >= head_i->data) {\r\n                arr[i] = head_i->next;\r\n                head_i->next = head_0;\r\n                arr[0] = head_i;\r\n            }\r\n            else\r\n                \/\/ Traverse the first list\r\n                while (head_0->next != NULL) {\r\n                    \/\/ Smaller than next element\r\n                    if (head_0->next->data\r\n                        >= head_i->data) {\r\n                        arr[i] = head_i->next;\r\n                        head_i->next = head_0->next;\r\n                        head_0->next = head_i;\r\n                        break;\r\n                    }\r\n                    \/\/ go to next node\r\n                    head_0 = head_0->next;\r\n \r\n                    \/\/ if last node\r\n                    if (head_0->next == NULL) {\r\n                        arr[i] = head_i->next;\r\n                        head_i->next = NULL;\r\n                        head_0->next = head_i;\r\n                        head_0->next->next = NULL;\r\n                        break;\r\n                    }\r\n                }\r\n        }\r\n    }\r\n \r\n    return arr[0];\r\n}\r\n \r\n\/\/ Utility function to create a new node.\r\nstruct Node* newNode(int data)\r\n{\r\n    struct Node *temp = (struct Node *) malloc(sizeof(struct Node));\r\n    temp->data = data;\r\n    temp->next = NULL;\r\n    return temp;\r\n}\r\n \r\n\/\/ Driver program to test\r\n\/\/ above functions\r\nint main()\r\n{\r\n    \/\/ Number of linked lists\r\n    int k = 3;\r\n \r\n    \/\/ Number of elements in each list\r\n    int n = 4;\r\n \r\n    \/\/ an array of pointers storing the\r\n    \/\/ head nodes of the linked lists\r\n    struct Node* arr[k];\r\n \r\n    arr[0] = newNode(10);\r\n    arr[0]->next = newNode(30);\r\n    arr[0]->next->next = newNode(50);\r\n    arr[0]->next->next->next = newNode(70);\r\n \r\n    arr[1] = newNode(20);\r\n    arr[1]->next = newNode(40);\r\n    arr[1]->next->next = newNode(60);\r\n    arr[1]->next->next->next = newNode(80);\r\n \r\n    arr[2] = newNode(0);\r\n    arr[2]->next = newNode(90);\r\n    arr[2]->next->next = newNode(100);\r\n    arr[2]->next->next->next = newNode(11);\r\n \r\n    \/\/ Merge all lists\r\n    struct Node* head = mergeKLists(arr, k - 1);\r\n \r\n    printList(head);\r\n \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_8516_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<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;\r\n    \/\/ constructor\r\n    Node(int x){\r\n        data = x;\r\n        next = NULL;\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\";\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 = l1->next;\r\n            } else {\r\n                tail->next = l2;\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        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    Node *head = new Node(2);\r\n    head->next = new Node(3);\r\n    head->next->next = new Node(5);\r\n    Node *h2 = new Node(1);\r\n    h2->next = new Node(4);\r\n    h2->next->next = new Node(8);\r\n\r\n    Node *h3 = new Node(6);\r\n    h3->next = new Node(7);\r\n    h3->next->next = new Node(9);\r\n\r\n    arr[0] = head;\r\n    arr[1] = h2;\r\n    arr[2] = h3;\r\n    cout<<\"Final resultant merged linked list\"<<endl;\r\n    print(mergeKLists(arr,2));\r\n    \r\n    \r\n    return 0;\r\n}\r\n\r\n\r\n\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_8516_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 Node {\r\n\tint data;\r\n\tNode next;\r\n\tNode(int data)\r\n\t{\r\n\t\tthis.data = data;\r\n\t}\r\n}\r\nclass MergeK \r\n{\r\n\tpublic static Node SortedMerge(Node a, Node b)\r\n\t{\r\n\t\tNode result = null;\r\n\t\t\/* Base cases *\/\r\n\t\tif (a == null)\r\n\t\t\treturn b;\r\n\t\telse if (b == null)\r\n\t\t\treturn a;\r\n\r\n\t\t\/* Pick either a or b, and recur *\/\r\n\t\tif (a.data <= b.data) {\r\n\t\t\tresult = a;\r\n\t\t\tresult.next = SortedMerge(a.next, b);\r\n\t\t}\r\n\t\telse {\r\n\t\t\tresult = b;\r\n\t\t\tresult.next = SortedMerge(a, b.next);\r\n\t\t}\r\n\r\n\t\treturn result;\r\n\t}\r\n\r\n\t\/\/ The main function that takes an array of lists\r\n\t\/\/ arr[0..last] and generates the sorted output\r\n\tpublic static Node mergeKLists(Node arr[], int last)\r\n\t{\r\n\t\t\/\/ repeat until only one list is left\r\n\t\twhile (last != 0) {\r\n\t\t\tint i = 0, j = last;\r\n\r\n\t\t\t\/\/ (i, j) forms a pair\r\n\t\t\twhile (i < j) {\r\n\t\t\t\t\/\/ merge List i with List j and store\r\n\t\t\t\t\/\/ merged list in List i\r\n\t\t\t\tarr[i] = SortedMerge(arr[i], arr[j]);\r\n\r\n\t\t\t\t\/\/ consider next pair\r\n\t\t\t\ti++;\r\n\t\t\t\tj--;\r\n\r\n\t\t\t\t\/\/ If all pairs are merged, update last\r\n\t\t\t\tif (i >= j)\r\n\t\t\t\t\tlast = j;\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\treturn arr[0];\r\n\t}\r\n\tpublic static void printList(Node node)\r\n\t{\r\n\t\twhile (node != null) {\r\n\t\t\tSystem.out.print(node.data + \" \");\r\n\t\t\tnode = node.next;\r\n\t\t}\r\n\t}\r\n\tpublic static void main(String args[])\r\n\t{\r\n\t\tint k = 3; \/\/ Number of linked lists\r\n\t\tint n = 4; \/\/ Number of elements in each list\r\n\r\n\t\t\/\/ an array of pointers storing the head nodes\r\n\t\t\/\/ of the linked lists\r\n\t\tNode arr[] = new Node[k];\r\n\r\n\t\tarr[0] = new Node(1);\r\n\t\tarr[0].next = new Node(3);\r\n\t\tarr[0].next.next = new Node(5);\r\n\t\tarr[0].next.next.next = new Node(7);\r\n\r\n\t\tarr[1] = new Node(2);\r\n\t\tarr[1].next = new Node(4);\r\n\t\tarr[1].next.next = new Node(6);\r\n\t\tarr[1].next.next.next = new Node(8);\r\n\r\n\t\tarr[2] = new Node(0);\r\n\t\tarr[2].next = new Node(9);\r\n\t\tarr[2].next.next = new Node(10);\r\n\t\tarr[2].next.next.next = new Node(11);\r\n\r\n\t\t\/\/ Merge all lists\r\n\t\tNode head = mergeKLists(arr, k - 1);\r\n\t\tprintList(head);\r\n\t}\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_8516_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# A Linked List node\r\nclass Node:\r\n\r\n    def __init__(self, x):\r\n    \r\n        self.data = x\r\n        self.next = None\r\n\r\n# Function to print nodes in\r\n# a given linked list\r\ndef printList(node):\r\n\r\n    while (node != None):\r\n        print(node.data,\r\n            end = \" \")\r\n        node = node.next\r\n\r\ndef mergeKLists(arr, last):\r\n\r\n    for i in range(1, last + 1):\r\n        while (True):\r\n            head_0 = arr[0]\r\n            head_i = arr[i]\r\n\r\n            if (head_i == None):\r\n                break\r\n\r\n            if (head_0.data >=\r\n                head_i.data):\r\n                arr[i] = head_i.next\r\n                head_i.next = head_0\r\n                arr[0] = head_i\r\n            else:\r\n                while (head_0.next != None):\r\n                    if (head_0.next.data >=\r\n                        head_i.data):\r\n                        arr[i] = head_i.next\r\n                        head_i.next = head_0.next\r\n                        head_0.next = head_i\r\n                        break\r\n                    head_0 = head_0.next\r\n                    if (head_0.next == None):\r\n                        arr[i] = head_i.next\r\n                        head_i.next = None\r\n                        head_0.next = head_i\r\n                        head_0.next.next = None\r\n                        break\r\n    return arr[0]\r\n\r\nif __name__ == '__main__':\r\n\r\n    k = 3\r\n    arr = [None for i in range(k)]\r\n\r\n    arr[0] = Node(2)\r\n    arr[0].next = Node(3)\r\n    arr[0].next.next = Node(5)\r\n\r\n    arr[1] = Node(1)\r\n    arr[1].next = Node(4)\r\n    arr[1].next.next = Node(8)\r\n\r\n    arr[2] = Node(6)\r\n    arr[2].next = Node(7)\r\n    arr[2].next.next = Node(9)\r\n\r\n    head = mergeKLists(arr, k - 1)\r\n\r\n    print(\"Final resultant merged linked list\")\r\n    printList(head)\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_8516 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_8516 a\"),jQuery(\"#tab-content_8516\"));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>Final resultant merged linked list<br \/>\n1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 5 -&gt; 6 -&gt; 7 -&gt; 8 -&gt; 9 -&gt; NULL<\/p>\n<p><strong>Time Complexity:<\/strong> O(NKlog(K)), <strong>N<\/strong> is the number of nodes in the list, and <strong>K<\/strong> is the total number of lists<br \/>\n[forminator_quiz id=&#8221;5128&#8243;]<\/p>\n<p>There is also a min heap based solution to this problem, to check out the heap based solution please visit [Merge K sorted linked lists using min heap](Merge k sorted linked lists Set 2 Using Min Heap).<\/p>\n<p>So, in this blog, we have tried to explain how you can merge <strong>K<\/strong> sorted linked lists in the most optimal way. If you want to practice more questions on linked lists, feel free to solve them at  Prepbytes <a href=\"https:\/\/mycode.prepbytes.com\/interview-coding\/practice\/linked-list\">Linked List<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction The linked list is one of the most important concepts to know while preparing for interviews. Having a good grasp of a linked list can be a huge plus point in coding interviews. Problem Statement In this problem, we will be given K sorted linked lists. We need to merge all lists such that [&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-5127","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 linked lists - Set 1 | Linked List | Prepbytes<\/title>\n<meta name=\"description\" content=\"Learn to merge k sorted lists into one sorted list.\" \/>\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-linked-lists-set-1\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Merge K sorted linked lists - Set 1 | Linked List | Prepbytes\" \/>\n<meta property=\"og:description\" content=\"Learn to merge k sorted lists into one sorted list.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/merge-k-sorted-linked-lists-set-1\/\" \/>\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-21T07:51:27+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-06-15T11:12:24+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1655291503614-1645007426664-Article_157.png\" \/>\n<meta name=\"author\" content=\"Prepbytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prepbytes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-k-sorted-linked-lists-set-1\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-k-sorted-linked-lists-set-1\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Merge K sorted linked lists | Set 1\",\"datePublished\":\"2021-09-21T07:51:27+00:00\",\"dateModified\":\"2022-06-15T11:12:24+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-k-sorted-linked-lists-set-1\/\"},\"wordCount\":901,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-k-sorted-linked-lists-set-1\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1655291503614-1645007426664-Article_157.png\",\"articleSection\":[\"Linked list articles\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/merge-k-sorted-linked-lists-set-1\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-k-sorted-linked-lists-set-1\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/merge-k-sorted-linked-lists-set-1\/\",\"name\":\"Merge K sorted linked lists - Set 1 | Linked List | Prepbytes\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-k-sorted-linked-lists-set-1\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-k-sorted-linked-lists-set-1\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1655291503614-1645007426664-Article_157.png\",\"datePublished\":\"2021-09-21T07:51:27+00:00\",\"dateModified\":\"2022-06-15T11:12:24+00:00\",\"description\":\"Learn to merge k sorted lists into one sorted list.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-k-sorted-linked-lists-set-1\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/merge-k-sorted-linked-lists-set-1\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-k-sorted-linked-lists-set-1\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1655291503614-1645007426664-Article_157.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1655291503614-1645007426664-Article_157.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-k-sorted-linked-lists-set-1\/#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 linked lists | Set 1\"}]},{\"@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 linked lists - Set 1 | Linked List | Prepbytes","description":"Learn to merge k sorted lists into one sorted list.","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-linked-lists-set-1\/","og_locale":"en_US","og_type":"article","og_title":"Merge K sorted linked lists - Set 1 | Linked List | Prepbytes","og_description":"Learn to merge k sorted lists into one sorted list.","og_url":"https:\/\/prepbytes.com\/blog\/merge-k-sorted-linked-lists-set-1\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2021-09-21T07:51:27+00:00","article_modified_time":"2022-06-15T11:12:24+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1655291503614-1645007426664-Article_157.png","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/merge-k-sorted-linked-lists-set-1\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/merge-k-sorted-linked-lists-set-1\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Merge K sorted linked lists | Set 1","datePublished":"2021-09-21T07:51:27+00:00","dateModified":"2022-06-15T11:12:24+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/merge-k-sorted-linked-lists-set-1\/"},"wordCount":901,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/merge-k-sorted-linked-lists-set-1\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1655291503614-1645007426664-Article_157.png","articleSection":["Linked list articles"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/merge-k-sorted-linked-lists-set-1\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/merge-k-sorted-linked-lists-set-1\/","url":"https:\/\/prepbytes.com\/blog\/merge-k-sorted-linked-lists-set-1\/","name":"Merge K sorted linked lists - Set 1 | Linked List | Prepbytes","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/merge-k-sorted-linked-lists-set-1\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/merge-k-sorted-linked-lists-set-1\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1655291503614-1645007426664-Article_157.png","datePublished":"2021-09-21T07:51:27+00:00","dateModified":"2022-06-15T11:12:24+00:00","description":"Learn to merge k sorted lists into one sorted list.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/merge-k-sorted-linked-lists-set-1\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/merge-k-sorted-linked-lists-set-1\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/merge-k-sorted-linked-lists-set-1\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1655291503614-1645007426664-Article_157.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1655291503614-1645007426664-Article_157.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/merge-k-sorted-linked-lists-set-1\/#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 linked lists | Set 1"}]},{"@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\/5127","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=5127"}],"version-history":[{"count":8,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/5127\/revisions"}],"predecessor-version":[{"id":8518,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/5127\/revisions\/8518"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=5127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=5127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=5127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}