{"id":5850,"date":"2021-10-26T09:12:06","date_gmt":"2021-10-26T09:12:06","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=5850"},"modified":"2022-12-14T07:35:09","modified_gmt":"2022-12-14T07:35:09","slug":"merge-two-unsorted-linked-lists-to-get-a-sorted-list","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/merge-two-unsorted-linked-lists-to-get-a-sorted-list\/","title":{"rendered":"Merge Two Unsorted Linked Lists To Get A Sorted List"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012290649-Article_206.png\" alt=\"\" \/><\/p>\n<h3>Introduction<\/h3>\n<p>Even though  we have seen different operations on linked lists, where we have done insertion, deletion etc. But what if we want to merge two unsorted linked lists. Lets just see an approach on how to merge 2 unsorted linked lists<\/p>\n<h3>Problem Statement on how to  merge two unsorted linked lists<\/h3>\n<p>In this problem, we are given two unsorted linked lists, and we have to merge these two linked lists to get a sorted linked list.<\/p>\n<p><strong>Prerequisites:<\/strong> Bubble sort on linked list.<\/p>\n<h3>Problem Statement Understanding  merge two unsorted linked lists<\/h3>\n<p>Let\u2019s try to understand the problem statement with the help of examples.<\/p>\n<p>Let\u2019s say the given linked lists are :<\/p>\n<p>List1 = head \u2192 1 \u2192 6 \u2192 10 \u2192 NULL ,<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/10\/p_1-5.jpg\" alt=\"\" \/><\/p>\n<p>List2 = head \u2192 5 \u2192 3 \u2192 9 \u2192 NULL <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/10\/p_2-6.jpg\" alt=\"\" \/><\/p>\n<ul>\n<li>So now, according to the problem statement, the given linked lists List1 and List2 are unsorted, and we need to merge them to get a sorted linked list.<\/li>\n<li>After merging the linked lists, we need to return the head of the merged linked list.<\/li>\n<li>After merging the two linked lists in the above example, the sorted linked lists will be head \u2192 1 \u2192 3 \u2192 5 \u2192 6 \u2192 9 \u2192 10 \u2192 NULL. <\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/10\/p_3-4.jpg\" alt=\"\" \/><\/p>\n<p>The linked list will be sorted in ascending order.<\/p>\n<h4>Some more Examples<\/h4>\n<p><strong>Example 1:<\/strong><\/p>\n<p>Input:<\/p>\n<p>List1 = head \u2192 4 \u2192 50\u2192 12 \u2192 NULL<\/p>\n<p>List2 = head \u2192 10 \u21921\u2192 60 \u2192 NULL<\/p>\n<p>Output :<\/p>\n<p>head \u2192 1 \u2192 4 \u2192 10\u219212 \u2192 50 \u2192 60<\/p>\n<p><strong>Example 2:<\/strong><\/p>\n<p>Input:<\/p>\n<p>List 1 =  head \u2192 10 \u2192 20 \u2192 60 \u2192 NULL<\/p>\n<p>List 2 = head \u2192 10 \u2192 50 \u2192 30 \u2192 NULL<\/p>\n<p>Output:<\/p>\n<p>head \u219210 \u2192 10 \u2192 20 \u2192 30 \u2192 50 \u2192 60 \u2192 NULL<\/p>\n<p>At this point, we have understood the problem statement. Now we will try to formulate an approach on how to merge 2 unsorted linked lists .<\/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>Naive Approach on how to merge two unsorted linked lists.<\/h3>\n<p>The naive approach to solve this problem is to first sort the two linked lists and then merge these sorted linked lists into one linked list in increasing order.<\/p>\n<p><strong>Time Complexity of how to merge 2 unsorted linked lists<\/strong><\/p>\n<p>O(N<sup>2<\/sup> + M<sup>2<\/sup> + (N+M)), where M and N are the lengths of the linked lists. We are first sorting both the lists. Sorting a list takes quadratic time (using bubble sort for sorting). Then we will be merging the two sorted list which will take O(N+M) time. Hence, the total time complexity will be O(N<sup>2<\/sup> + M<sup>2<\/sup> + (N+M)).<\/p>\n<p><strong>Space Complexity of how to merge two unsorted linked lists:<\/strong><\/p>\n<p>0(1), We are using only a constant amount of extra space.<\/p>\n<p><strong>Note:<\/strong> To see how to merge two sorted linked lists in one linked list in increasing order, check out this <a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/merge-two-sorted-linked-lists\/\">article<\/a>.<\/p>\n<h3>Efficient Approach on how to merge two unsorted linked lists<\/h3>\n<p>In the efficient approach, we will first concatenate the two given linked lists and then sort the final concatenated linked list by using a sorting algorithm.<\/p>\n<p>Here we will be using bubble sort to sort the linked list. To see how bubble sort works on a linked list, check out this <a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/bubble-sort-for-linked-list-by-swapping-nodes\/\">article<\/a>.<\/p>\n<h3>Algorithm on how to merge 2 unsorted linked lists<\/h3>\n<p>The algorithm of this problem is below:<\/p>\n<p>1) First, we will concatenate the two lists. <\/p>\n<ul>\n<li>In the first list, we will traverse the list until we reach its tail, and after reaching the tail, we will point the next of the tail node to the head node of the second list, storing the concatenated in the first list.\n<p>2) Then, we will sort this merged list (we will use bubble sort). <\/li>\n<li>While sorting, if node\u2192next\u2192data is less than node\u2192data, then we will swap the data.<\/li>\n<\/ul>\n<h3>Dry Run on how to merge two unsorted linked lists<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/10\/p_4-2.jpg\" alt=\"\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/10\/p_5-1.jpg\" alt=\"\" \/><\/p>\n<h3>Code Implementation on how to merge 2 unsorted linked lists<\/h3>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_5851 {\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_5851 .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_5851 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_5851 .wpsm_nav-tabs > li.active > a, #tab_container_5851 .wpsm_nav-tabs > li.active > a:hover, #tab_container_5851 .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_5851 .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_5851 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_5851 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_5851 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_5851 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_5851 .wpsm_nav-tabs > li > a:hover , #tab_container_5851 .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_5851 .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_5851 .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_5851 .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_5851 .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_5851 .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_5851 .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_5851 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_5851 .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_5851 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_5851 .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_5851 .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_5851\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_5851\">\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_5851_1\" aria-controls=\"tabs_desc_5851_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_5851_2\" aria-controls=\"tabs_desc_5851_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_5851_3\" aria-controls=\"tabs_desc_5851_3\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Python<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_5851\">\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_5851_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&lt;stdio.h&gt;\r\n#include&lt;stdlib.h&gt;\r\n\r\nstruct node {\r\n    int data;\r\n    struct node* next;\r\n};\r\n \r\n\/\/ Function to print the linked list\r\nvoid setData(struct node* head)\r\n{\r\n    struct node* tmp;\r\n \r\n    \/\/ Store the head of the linked\r\n    \/\/ list into a temporary node*\r\n    \/\/ and iterate\r\n    tmp = head;\r\n \r\n    while (tmp != NULL) {\r\n \r\n        printf(&quot;%d-&gt;&quot;,tmp-&gt;data);\r\n        tmp = tmp-&gt;next;\r\n    }\r\n}\r\n \r\n\/\/ Function takes the head of the\r\n\/\/ LinkedList and the data as\r\n\/\/ argument and if no LinkedList\r\n\/\/ exists, it creates one with the\r\n\/\/ head pointing to first node.\r\n\/\/ If it exists already, it appends\r\n\/\/ given node at end of the last node\r\nstruct node* getData(struct node* head, int num)\r\n{\r\n \r\n    \/\/ Create a new node\r\n    struct node *temp = (struct node *) malloc(sizeof(struct node));\r\n    struct node* tail = head;\r\n \r\n    \/\/ Insert data into the temporary\r\n    \/\/ node and point it's next to NULL\r\n    temp-&gt;data = num;\r\n    temp-&gt;next = NULL;\r\n \r\n    \/\/ Check if head is null, create a\r\n    \/\/ linked list with temp as head\r\n    \/\/ and tail of the list\r\n    if (head == NULL) {\r\n        head = temp;\r\n        tail = temp;\r\n    }\r\n \r\n    \/\/ Else insert the temporary node\r\n    \/\/ after the tail of the existing\r\n    \/\/ node and make the temporary node\r\n    \/\/ as the tail of the linked list\r\n    else {\r\n \r\n        while (tail != NULL) {\r\n \r\n            if (tail-&gt;next == NULL) {\r\n                tail-&gt;next = temp;\r\n                tail = tail-&gt;next;\r\n            }\r\n            tail = tail-&gt;next;\r\n        }\r\n    }\r\n \r\n    \/\/ Return the list\r\n    return head;\r\n}\r\n \r\n\/\/ Function to concatenate the two lists\r\nstruct node* mergelists(struct node** head1,\r\n                 struct node** head2)\r\n{\r\n \r\n    struct node* tail = *head1;\r\n \r\n    \/\/ Iterate through the head1 to find the\r\n    \/\/ last node join the next of last node\r\n    \/\/ of head1 to the 1st node of head2\r\n    while (tail != NULL) {\r\n \r\n        if (tail-&gt;next == NULL\r\n            &amp;&amp; head2 != NULL) {\r\n            tail-&gt;next = *head2;\r\n            break;\r\n        }\r\n        tail = tail-&gt;next;\r\n    }\r\n \r\n    \/\/ return the concatenated lists as a\r\n    \/\/ single list - head1\r\n    return *head1;\r\n}\r\n \r\n\/\/ Sort the linked list using bubble sort\r\nvoid sortlist(struct node** head1)\r\n{\r\n    struct node* curr = *head1;\r\n    struct node* temp = *head1;\r\n \r\n    \/\/ Compares two adjacent elements\r\n    \/\/ and swaps if the first element\r\n    \/\/ is greater than the other one.\r\n    while (curr-&gt;next != NULL) {\r\n \r\n        temp = curr-&gt;next;\r\n        while (temp != NULL) {\r\n \r\n            if (temp-&gt;data &lt; curr-&gt;data) {\r\n                int t = temp-&gt;data;\r\n                temp-&gt;data = curr-&gt;data;\r\n                curr-&gt;data = t;\r\n            }\r\n            temp = temp-&gt;next;\r\n        }\r\n        curr = curr-&gt;next;\r\n    }\r\n}\r\n \r\n\/\/ Driver Code\r\nint main()\r\n{\r\n    struct node* head1;\r\n    struct node* head2;\r\n \r\n    head1 = NULL;\r\n    head2 = NULL;\r\n \r\n    \/\/ Given Linked List 1\r\n    head1 = getData(head1, 40);\r\n    head1 = getData(head1, 72);\r\n    head1 = getData(head1, 50);\r\n \r\n    \/\/ Given Linked List 2\r\n    head2 = getData(head2, 20);\r\n    head2 = getData(head2, 11);\r\n    head2 = getData(head2, 8);\r\n    head2 = getData(head2, 1);\r\n \r\n    \/\/ Merge the two lists\r\n    \/\/ in a single list\r\n    head1 = mergelists(&amp;head1,\r\n                       &amp;head2);\r\n \r\n    \/\/ Sort the unsorted merged list\r\n    sortlist(&amp;head1);\r\n \r\n    \/\/ Print the final\r\n    \/\/ sorted merged list\r\n    setData(head1);\r\n    return 0;\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_5851_2\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"java\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\npublic class Prepbytes {\r\n\r\n    static node head1 = null;\r\n    static node head2 = null;\r\n\r\n    static class node {\r\n        int data;\r\n        node next;\r\n    };\r\n\r\n    static void printList(node head) {\r\n        node tmp;\r\n\r\n        tmp = head;\r\n\r\n        while (tmp.next != null) {\r\n            System.out.print(tmp.data + &quot; -&gt; &quot;);\r\n            tmp = tmp.next;\r\n        }\r\n        System.out.println(tmp.data );\r\n    }\r\n\r\n    static node AddNode(node head, int num) {\r\n        \/\/ Creating a new node\r\n        node temp = new node();\r\n        node tail = head;\r\n\r\n        temp.data = num;\r\n        temp.next = null;\r\n\r\n        \/\/ If head is null then create linked list\r\n        \/\/ with temp as tail and head of the linked list\r\n        if (head == null) {\r\n            head = temp;\r\n            tail = temp;\r\n        }\r\n\r\n        \/\/ Else insert the temp node\r\n        \/\/ after the tail of the existing\r\n        \/\/ node and make the temp node\r\n        \/\/ as the tail of the linked list\r\n        else {\r\n            while (tail != null) {\r\n                if (tail.next == null) {\r\n                    tail.next = temp;\r\n                    tail = tail.next;\r\n                }\r\n                tail = tail.next;\r\n            }\r\n        }\r\n\r\n        \/\/ Return the list\r\n        return head;\r\n    }\r\n\r\n    \/\/ This function will concatenate\r\n    \/\/ the two linked list\r\n    static node concatenateList() {\r\n        node tail = head1;\r\n\r\n        while (tail != null) {\r\n            if (tail.next == null &amp;&amp; head2 != null) {\r\n                tail.next = head2;\r\n                break;\r\n            }\r\n            tail = tail.next;\r\n        }\r\n\r\n        return head1;\r\n    }\r\n\r\n    \/\/ This function will sort the linked list\r\n    static void sort() {\r\n        node current = head1;\r\n        node temp = head1;\r\n\r\n        \/\/ Compares the elements\r\n        \/\/ If node-&gt;next-&gt;data is less than node-&gt;data\r\n        \/\/ then we will swap the data.\r\n        while (current.next != null) {\r\n            temp = current.next;\r\n            while (temp != null) {\r\n                if (temp.data &lt; current.data) {\r\n                    int t = temp.data;\r\n                    temp.data = current.data;\r\n                    current.data = t;\r\n                }\r\n                temp = temp.next;\r\n            }\r\n            current = current.next;\r\n        }\r\n    }\r\n\r\n    public static void main(String[] args) {\r\n        \/\/ First linked list\r\n        head1 = AddNode(head1, 1);\r\n        head1 = AddNode(head1, 6);\r\n        head1 = AddNode(head1, 10);\r\n        System.out.println(&quot;List1:&quot;);\r\n        printList(head1);\r\n        \/\/ Second Linked list\r\n        head2 = AddNode(head2, 5);\r\n        head2 = AddNode(head2, 3);\r\n        head2 = AddNode(head2, 9);\r\n        System.out.println(&quot;List2:&quot;);\r\n        printList(head2);\r\n\r\n        head1 = concatenateList();\r\n\r\n        sort();\r\n        System.out.println(&quot;Final merged list:&quot;);\r\n        printList(head1);\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_5851_3\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"Python\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"Python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\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\ndef setData(head):\r\n\r\n    tmp = head\r\n\r\n    while (tmp != None):\r\n        print(tmp.data,\r\n            end = &quot; -&gt; &quot;)\r\n        tmp = tmp.next\r\n\r\ndef getData(head, num):\r\n\r\n    temp = node(-1)\r\n    tail = head\r\n    temp.data = num\r\n    temp.next = None\r\n\r\n    if (head == None):\r\n        head = temp\r\n        tail = temp\r\n\r\n    else:\r\n        while (tail != None):\r\n            if (tail.next == None):\r\n                tail.next = temp\r\n                tail = tail.next\r\n            tail = tail.next\r\n\r\n    return head\r\n\r\ndef mergelists(head1,head2):\r\n\r\n    tail = head1\r\n    while (tail != None):\r\n        if (tail.next == None\r\n            and head2 != None):\r\n            tail.next =head2\r\n            break\r\n        tail = tail.next\r\n\r\n    return head1\r\n\r\ndef sortlist(head1):\r\n\r\n    curr = head1\r\n    temp = head1\r\n\r\n    while (curr.next != None):\r\n        temp = curr.next\r\n        while (temp != None):\r\n            if (temp.data &lt; curr.data):\r\n                t = temp.data\r\n                temp.data = curr.data\r\n                curr.data = t\r\n            temp = temp.next\r\n        curr = curr.next\r\n\r\nif __name__ == '__main__':\r\n\r\n    head1 = None\r\n    head2 = None\r\n\r\n    head1 = getData(head1, 1)\r\n    head1 = getData(head1, 6)\r\n    head1 = getData(head1, 10)\r\n\r\n    head2 = getData(head2, 5)\r\n    head2 = getData(head2, 3)\r\n    head2 = getData(head2, 9)\r\n\r\n    head1 = mergelists(head1,head2)\r\n\r\n    sortlist(head1)\r\n    setData(head1)\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_5851 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_5851 a\"),jQuery(\"#tab-content_5851\"));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>List1:<\/p>\n<p>1 -&gt; 6 -&gt; 10<\/p>\n<p>List2:<\/p>\n<p>5 -&gt; 3 -&gt; 9<\/p>\n<p>Final merged list:<\/p>\n<p>1 -&gt; 3 -&gt; 5 -&gt; 6 -&gt; 9 -&gt; 10<\/p>\n<p><strong>Time Complexity<\/strong><\/p>\n<p>O((N+M)<sup>2<\/sup>), where M and N are the lengths of the linked lists. We are merging given linked lists and iterating the merged list using nested loops to perform bubble sort. The time complexity of bubble sort is O((list.length)<sup>2<\/sup>). Hence, the time complexity will be O(((N+M)<sup>2<\/sup>).<\/p>\n<p><strong>Space Complexity of how to merge two unsorted linked lists<\/strong><\/p>\n<p><strong>Note:<\/strong> Also note that to reduce the time complexity, we can use merge sort instead of bubble sort. Merge sort will have a time complexity of O(N * logN), whereas bubble sort has complexity of O(N<sup>2<\/sup>).<\/p>\n<p>So, in this blog, we have tried to explain how you can merge two unsorted linked lists to get a sorted linked list. We understood an efficient approach by analyzing algorithm and approach individually on how to merge two unsorted linked listsIf you want to solve more questions on Linked List, which are curated by our expert mentors at PrepBytes, you can follow this link <a href=\"https:\/\/mycode.prepbytes.com\/interview-coding\/practice\/linked-list\">Linked List<\/a>.<\/p>\n<h2> FAQs on how to merge two unsorted linked lists <\/h2>\n<ol>\n<li><strong>Why merge sorting is better in linked lists?<\/strong><\/li>\n<p>Generally, in linked lists we can insert elements wherever we can if we have a reference point, even though it requires extra spaces only merge sort can be implemented with or without extra space for linked lists.<\/p>\n<li><strong>Which is better Quick sort or Merge sort?<\/strong><\/li>\n<p>Quick sort is better than merge sort because quicksort does not require any extra space for merging whereas merge sort requires as it needs an empty list to merge.<\/p>\n<li><strong>Which sorting methods are stable?<\/strong><\/li>\n<p>Sortings such as merge sort, tim sort, counting sort, insertion sort and bubble sort are stable.<\/p>\n<li><strong>Which sorting methods are unstable? <\/strong><\/li>\n<p>Sortings such as Quick sort, heap sort and Selection sort are unstable.<\/ol><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Even though we have seen different operations on linked lists, where we have done insertion, deletion etc. But what if we want to merge two unsorted linked lists. Lets just see an approach on how to merge 2 unsorted linked lists Problem Statement on how to merge two unsorted linked lists In this problem, [&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-5850","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 Unsorted Linked Lists To Get A Sorted List | Linked List | Prepbytes<\/title>\n<meta name=\"description\" content=\"Learn the most effective way to merge two unsorted linked lists to get a 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-two-unsorted-linked-lists-to-get-a-sorted-list\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Merge Two Unsorted Linked Lists To Get A Sorted List | Linked List | Prepbytes\" \/>\n<meta property=\"og:description\" content=\"Learn the most effective way to merge two unsorted linked lists to get a sorted list.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/merge-two-unsorted-linked-lists-to-get-a-sorted-list\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-10-26T09:12:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-12-14T07:35:09+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012290649-Article_206.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-unsorted-linked-lists-to-get-a-sorted-list\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-two-unsorted-linked-lists-to-get-a-sorted-list\/\"},\"author\":{\"name\":\"PrepBytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e\"},\"headline\":\"Merge Two Unsorted Linked Lists To Get A Sorted List\",\"datePublished\":\"2021-10-26T09:12:06+00:00\",\"dateModified\":\"2022-12-14T07:35:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-two-unsorted-linked-lists-to-get-a-sorted-list\/\"},\"wordCount\":936,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-two-unsorted-linked-lists-to-get-a-sorted-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012290649-Article_206.png\",\"articleSection\":[\"Linked list articles\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/merge-two-unsorted-linked-lists-to-get-a-sorted-list\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-two-unsorted-linked-lists-to-get-a-sorted-list\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/merge-two-unsorted-linked-lists-to-get-a-sorted-list\/\",\"name\":\"Merge Two Unsorted Linked Lists To Get A Sorted List | Linked List | Prepbytes\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-two-unsorted-linked-lists-to-get-a-sorted-list\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-two-unsorted-linked-lists-to-get-a-sorted-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012290649-Article_206.png\",\"datePublished\":\"2021-10-26T09:12:06+00:00\",\"dateModified\":\"2022-12-14T07:35:09+00:00\",\"description\":\"Learn the most effective way to merge two unsorted linked lists to get a sorted list.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-two-unsorted-linked-lists-to-get-a-sorted-list\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/merge-two-unsorted-linked-lists-to-get-a-sorted-list\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-two-unsorted-linked-lists-to-get-a-sorted-list\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012290649-Article_206.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012290649-Article_206.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-two-unsorted-linked-lists-to-get-a-sorted-list\/#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 Unsorted Linked Lists To Get A Sorted List\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e\",\"name\":\"PrepBytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g\",\"caption\":\"PrepBytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/prepbytes\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Merge Two Unsorted Linked Lists To Get A Sorted List | Linked List | Prepbytes","description":"Learn the most effective way to merge two unsorted linked lists to get a 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-two-unsorted-linked-lists-to-get-a-sorted-list\/","og_locale":"en_US","og_type":"article","og_title":"Merge Two Unsorted Linked Lists To Get A Sorted List | Linked List | Prepbytes","og_description":"Learn the most effective way to merge two unsorted linked lists to get a sorted list.","og_url":"https:\/\/prepbytes.com\/blog\/merge-two-unsorted-linked-lists-to-get-a-sorted-list\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2021-10-26T09:12:06+00:00","article_modified_time":"2022-12-14T07:35:09+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012290649-Article_206.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-unsorted-linked-lists-to-get-a-sorted-list\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/merge-two-unsorted-linked-lists-to-get-a-sorted-list\/"},"author":{"name":"PrepBytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e"},"headline":"Merge Two Unsorted Linked Lists To Get A Sorted List","datePublished":"2021-10-26T09:12:06+00:00","dateModified":"2022-12-14T07:35:09+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/merge-two-unsorted-linked-lists-to-get-a-sorted-list\/"},"wordCount":936,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/merge-two-unsorted-linked-lists-to-get-a-sorted-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012290649-Article_206.png","articleSection":["Linked list articles"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/merge-two-unsorted-linked-lists-to-get-a-sorted-list\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/merge-two-unsorted-linked-lists-to-get-a-sorted-list\/","url":"https:\/\/prepbytes.com\/blog\/merge-two-unsorted-linked-lists-to-get-a-sorted-list\/","name":"Merge Two Unsorted Linked Lists To Get A Sorted List | Linked List | Prepbytes","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/merge-two-unsorted-linked-lists-to-get-a-sorted-list\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/merge-two-unsorted-linked-lists-to-get-a-sorted-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012290649-Article_206.png","datePublished":"2021-10-26T09:12:06+00:00","dateModified":"2022-12-14T07:35:09+00:00","description":"Learn the most effective way to merge two unsorted linked lists to get a sorted list.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/merge-two-unsorted-linked-lists-to-get-a-sorted-list\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/merge-two-unsorted-linked-lists-to-get-a-sorted-list\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/merge-two-unsorted-linked-lists-to-get-a-sorted-list\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012290649-Article_206.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012290649-Article_206.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/merge-two-unsorted-linked-lists-to-get-a-sorted-list\/#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 Unsorted Linked Lists To Get A Sorted List"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e","name":"PrepBytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g","caption":"PrepBytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/prepbytes\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/5850","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=5850"}],"version-history":[{"count":9,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/5850\/revisions"}],"predecessor-version":[{"id":10325,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/5850\/revisions\/10325"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=5850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=5850"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=5850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}