{"id":4687,"date":"2021-09-02T09:49:12","date_gmt":"2021-09-02T09:49:12","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=4687"},"modified":"2022-03-10T18:24:27","modified_gmt":"2022-03-10T18:24:27","slug":"sort-a-linked-list-that-is-sorted-alternating-ascending-and-descending-orders","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/sort-a-linked-list-that-is-sorted-alternating-ascending-and-descending-orders\/","title":{"rendered":"Sort a linked list that is sorted alternating ascending and descending orders"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644926345566-114.Sort%20a%20linked%20list%20that%20is%20sorted%20alternating%20ascending%20and%20descending%20orders_Artboard%203.png\" alt=\"\" \/><\/p>\n<h3>Introduction<\/h3>\n<p>The linked list is one of the most important concepts and data structures to learn while preparing for interviews. Having a good grasp of Linked Lists can be a huge plus point in a coding interview.<\/p>\n<\/p>\n<h3>Problem Statement<\/h3>\n<p>According to the problem statement, our task is to sort a linked list that is sorted alternating ascending and descending orders.<\/p>\n<h3>Problem Statement Understanding<\/h3>\n<p>Let\u2019s try to understand the problem statement with of an example by referring competitive programming online course.<\/p>\n<p>We have been given a linked list which is sorted alternating ascending and descending orders. We can understand this statement with an example:<\/p>\n<p>1 \u2192 40 \u2192 5 \u2192 30 \u2192 6 \u2192 12 \u2192 20 \u2192 NULL<\/p>\n<ul>\n<li>1, 5, 6, 20 these nodes are present in the linked list in ascending order, and 40, 30, 12 are present in descending order in the linked list. These nodes are present in alternating positions in the linked list. <\/li>\n<li>Our task is to sort the linked list such that after sorting, the final sorted linked list will be:<br \/>\n1 \u2192 5 \u2192 6 \u2192 12 \u2192 20 \u2192 30 \u2192 40 \u2192 NULL<\/li>\n<\/ul>\n<p>Now I think from the above example, the problem statement is clear. So let&#8217;s see how we will approach it.<\/p>\n<p>A straightforward approach is to <a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/recursive-selection-sort-for-singly-linked-list-swapping-node-links\/\" title=\"sort\">sort<\/a> the linked list using the <strong>merge sort algorithm<\/strong>, but the only problem is that here we are not utilizing the information given to us in the problem that the linked list is already sorted in  alternating ascending and descending orders.<\/p>\n<p>If we are using merge sort to sort the linked list the time complexity will be O(NlogN), so now we will try to think can we utilize the information given to us in the problem and will try to solve the problem in lower time complexity.<\/p>\n<p>Before jumping to the approach, try to think how you can approach this problem. If stuck, no problem, we will thoroughly see how we can approach the problem in the next section.<\/p>\n<p>Let\u2019s move on to the approach section.<\/p>\n<h3>Approach<\/h3>\n<p>We have to divide the problem into three steps: <\/p>\n<ul>\n<li>The first step is to split the linked list into two linked lists. One list is for nodes in ascending order and the second is for nodes in descending order. <\/li>\n<li>After this step, We have to reverse the second linked list to change its order from descending to ascending. <\/li>\n<li>After this step our both linked lists are sorted in ascending order, Now merge both lists into a single list in such fashion that the final list will also remain sorted.<\/li>\n<\/ul>\n<h3>Algorithm<\/h3>\n<ul>\n<li>We have to divide this problem into three steps.<\/li>\n<li>The first step is to split the linked list into two lists.<\/li>\n<li>Initialize two pointer variables of the type node.<\/li>\n<li>One pointer for nodes of ascending order and another is for nodes in descending order.<\/li>\n<li>Now we will have two lists, one is sorted in ascending order and another list is sorted in descending order.<\/li>\n<li>Now reverse the second list. This will make the second linked list sorted in ascending order.<\/li>\n<li>Now we have two sorted linked lists.<\/li>\n<li>Merge both the linked list.<\/li>\n<li>The final linked list after merging both the linked lists will be our resultant linked list.<\/li>\n<\/ul>\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_4688 {\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_4688 .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_4688 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_4688 .wpsm_nav-tabs > li.active > a, #tab_container_4688 .wpsm_nav-tabs > li.active > a:hover, #tab_container_4688 .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_4688 .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_4688 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_4688 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_4688 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_4688 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_4688 .wpsm_nav-tabs > li > a:hover , #tab_container_4688 .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_4688 .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_4688 .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_4688 .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_4688 .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_4688 .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_4688 .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_4688 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_4688 .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_4688 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_4688 .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_4688 .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_4688\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_4688\">\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_4688_1\" aria-controls=\"tabs_desc_4688_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_4688_2\" aria-controls=\"tabs_desc_4688_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_4688_3\" aria-controls=\"tabs_desc_4688_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_4688\">\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_4688_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 &lt;bits stdc++.h=&quot;&quot;&gt;\r\nusing namespace std;\r\n\r\n\/\/ Linked list node\r\nstruct Node {\r\n   int data;\r\n   struct Node* next;\r\n};\r\n\r\nNode* mergelist(Node* head1, Node* head2);\r\nvoid splitList(Node* head, Node** Ahead, Node** Dhead);\r\nvoid reverselist(Node*&amp; head);\r\n\r\n\/\/ the function that sorts the\r\n\/\/ linked list\r\nvoid sort(Node** head){\r\n   \/\/ Split the linked list into lists\r\n   Node *Ahead, *Dhead;\r\n   splitList(*head, &amp;Ahead, &amp;Dhead);\r\n\r\n   \/\/ Reverse the descending ordered linked list into ascending order\r\n   reverselist(Dhead);\r\n\r\n   \/\/ Merge both linked lists \r\n   *head = mergelist(Ahead, Dhead);\r\n}\r\n\r\n\/\/ A function to create a new node\r\nNode* newNode(int data)\r\n{\r\n   Node* temp = new Node;\r\n   temp-&gt;data = data;\r\n   temp-&gt;next = NULL;\r\n   return temp;\r\n}\r\n\r\n\/\/ function to reverse a linked list\r\nvoid reverselist(Node*&amp; head){\r\n   Node *prev = NULL, *curr = head, *next;\r\n   while (curr) {\r\n      next = curr-&gt;next;\r\n      curr-&gt;next = prev;\r\n      prev = curr;\r\n      curr = next;\r\n   }\r\n   head = prev;\r\n}\r\n\r\n\/\/ function to print a linked list\r\nvoid printlist(Node* head){\r\n   while (head != NULL) {\r\n      cout &lt;&lt; head-&gt;data &lt;&lt; &quot; &quot;;\r\n      head = head-&gt;next;\r\n   }\r\n   cout &lt;&lt; endl;\r\n}\r\n\r\n\/\/ A function to merge two sorted linked lists\r\nNode* mergelist(Node* head1, Node* head2)\r\n{\r\n   \/\/ Base cases\r\n   if (!head1)\r\n      return head2;\r\n   if (!head2)\r\n      return head1;\r\n\r\n   Node* temp = NULL;\r\n   if (head1-&gt;data &lt; head2-&gt;data) {\r\n      temp = head1;\r\n      head1-&gt;next = mergelist(head1-&gt;next, head2);\r\n   }\r\n   else {\r\n      temp = head2;\r\n      head2-&gt;next = mergelist(head1, head2-&gt;next);\r\n   }\r\n   return temp;\r\n}\r\n\r\n\/\/ This function alternatively splits\r\n\/\/ a linked list into two lists.\r\n\/\/ &quot;Ahead&quot; is pointer to head of ascending linked list\r\n\/\/ &quot;Dhead&quot; is pointer to head of descending linked list\r\nvoid splitList(Node* head, Node** Ahead, Node** Dhead){\r\n   \/\/ Create two dummy nodes to initialize\r\n   \/\/ heads of two linked list\r\n   *Ahead = newNode(0);\r\n   *Dhead = newNode(0);\r\n\r\n   Node* ascn = *Ahead;\r\n   Node* dscn = *Dhead;\r\n   Node* curr = head;\r\n\r\n   \/\/ Link alternate nodes\r\n   while (curr) {\r\n      \/\/ Link alternate nodes of ascending linked list\r\n      ascn-&gt;next = curr;\r\n      ascn = ascn-&gt;next;\r\n      curr = curr-&gt;next;\r\n\r\n      \/\/ Link alternate nodes of descending linked list\r\n      if (curr) {\r\n         dscn-&gt;next = curr;\r\n         dscn = dscn-&gt;next;\r\n         curr = curr-&gt;next;\r\n      }\r\n   }\r\n\r\n   ascn-&gt;next = NULL;\r\n   dscn-&gt;next = NULL;\r\n   *Ahead = (*Ahead)-&gt;next;\r\n   *Dhead = (*Dhead)-&gt;next;\r\n}\r\n\r\nint main()\r\n{\r\n   Node* head = newNode(1);\r\n   head-&gt;next = newNode(9);\r\n   head-&gt;next-&gt;next = newNode(5);\r\n   head-&gt;next-&gt;next-&gt;next = newNode(8);\r\n   head-&gt;next-&gt;next-&gt;next-&gt;next = newNode(7);\r\n\r\n   cout &lt;&lt; &quot;Input linked list&quot; &lt;&lt; endl;\r\n   printlist(head);\r\n\r\n   sort(&amp;head);\r\n\r\n   cout &lt;&lt; &quot;Final Sorted Linked List &quot; &lt;&lt; endl;\r\n   printlist(head);\r\n\r\n   return 0;\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_4688_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\nclass Sort \r\n{\r\n    Node head; \r\n    class Node \r\n    {\r\n        int data;\r\n        Node next;\r\n        Node(int d)\r\n        {\r\n            data = d;\r\n            next = null;\r\n        }\r\n    }\r\n\r\n    Node newNode(int key)\r\n    {\r\n        return new Node(key);\r\n    }\r\n\r\n    \/* This is the main function that sorts\r\n    the linked list.*\/\r\n    void sort()\r\n    {\r\n        \/* Create 2 dummy nodes and initialise as\r\n        heads of linked lists *\/\r\n        Node Ahead = new Node(0), Dhead = new Node(0);\r\n\r\n        \/\/ Split the list into lists\r\n        splitList(Ahead, Dhead);\r\n\r\n        Ahead = Ahead.next;\r\n        Dhead = Dhead.next;\r\n\r\n        \/\/ reverse the descending list\r\n        Dhead = reverseList(Dhead);\r\n\r\n        \/\/ merge the 2 linked lists\r\n        head = mergeList(Ahead, Dhead);\r\n    }\r\n\r\n    \/* Function to reverse the linked list *\/\r\n    Node reverseList(Node Dhead)\r\n    {\r\n        Node current = Dhead;\r\n        Node prev = null;\r\n        Node next;\r\n        while (current != null) {\r\n            next = current.next;\r\n            current.next = prev;\r\n            prev = current;\r\n            current = next;\r\n        }\r\n        Dhead = prev;\r\n        return Dhead;\r\n    }\r\n    void printList()\r\n    {\r\n        Node temp = head;\r\n        while (temp != null) {\r\n            System.out.print(temp.data + &quot; &quot;);\r\n            temp = temp.next;\r\n        }\r\n        System.out.println();\r\n    }\r\n    \/\/ A utility function to merge two sorted linked lists\r\n    Node mergeList(Node head1, Node head2)\r\n    {\r\n        \/\/ Base cases\r\n        if (head1 == null)\r\n            return head2;\r\n        if (head2 == null)\r\n            return head1;\r\n\r\n        Node temp = null;\r\n        if (head1.data &lt; head2.data) {\r\n            temp = head1;\r\n            head1.next = mergeList(head1.next, head2);\r\n        }\r\n        else {\r\n            temp = head2;\r\n            head2.next = mergeList(head1, head2.next);\r\n        }\r\n        return temp;\r\n    }\r\n\r\n    \/\/ This function alternatively splits\r\n    \/\/ a linked list with head as head into two:\r\n    \/\/ For example, 10-&gt;20-&gt;30-&gt;15-&gt;40-&gt;7 is\r\n    \/\/ splitted into 10-&gt;30-&gt;40\r\n    \/\/ and 20-&gt;15-&gt;7\r\n    \/\/ &quot;Ahead&quot; is reference to head of ascending linked list\r\n    \/\/ &quot;Dhead&quot; is reference to head of descending linked list\r\n    void splitList(Node Ahead, Node Dhead)\r\n    {\r\n        Node ascn = Ahead;\r\n        Node dscn = Dhead;\r\n        Node curr = head;\r\n\r\n        \/\/ Link alternate nodes\r\n\r\n        while (curr != null) {\r\n            \/\/ Link alternate nodes in ascending order\r\n            ascn.next = curr;\r\n            ascn = ascn.next;\r\n            curr = curr.next;\r\n\r\n            if (curr != null) {\r\n                dscn.next = curr;\r\n                dscn = dscn.next;\r\n                curr = curr.next;\r\n            }\r\n        }\r\n\r\n        ascn.next = null;\r\n        dscn.next = null;\r\n    }\r\n\r\n    \/* Driver program to test above functions *\/\r\n    public static void main(String args[])\r\n    {\r\n        Sort llist = new Sort();\r\n        llist.head = llist.newNode(10);\r\n        llist.head.next = llist.newNode(40);\r\n        llist.head.next.next = llist.newNode(53);\r\n        llist.head.next.next.next = llist.newNode(30);\r\n        llist.head.next.next.next.next = llist.newNode(67);\r\n        llist.head.next.next.next.next.next = llist.newNode(12);\r\n        llist.head.next.next.next.next.next.next = llist.newNode(89);\r\n\r\n        System.out.println(&quot;Given linked list&quot;);\r\n        llist.printList();\r\n\r\n        llist.sort();\r\n\r\n        System.out.println(&quot;Sorted linked list&quot;);\r\n        llist.printList();\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_4688_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 LinkedList(object):\r\n    def __init__(self):\r\n        self.head = None\r\n\r\n    # Linked list Node\r\n    class Node(object):\r\n        def __init__(self, d):\r\n            self.data = d\r\n            self.next = None\r\n\r\n    def newNode(self, key):\r\n        return self.Node(key)\r\n\r\n    # This is the main function that sorts\r\n    # the linked list.\r\n    def sort(self):\r\n        # Create 2 dummy nodes and initialise as\r\n        # heads of linked lists\r\n        Ahead = self.Node(0)\r\n        Dhead = self.Node(0)\r\n        # Split the list into lists\r\n        self.splitList(Ahead, Dhead)\r\n        Ahead = Ahead.next\r\n        Dhead = Dhead.next\r\n        # reverse the descending list\r\n        Dhead = self.reverseList(Dhead)\r\n        # merge the 2 linked lists\r\n        self.head = self.mergeList(Ahead, Dhead)\r\n\r\n    # Function to reverse the linked list\r\n    def reverseList(self, Dhead):\r\n        current = Dhead\r\n        prev = None\r\n        while current != None:\r\n            self._next = current.next\r\n            current.next = prev\r\n            prev = current\r\n            current = self._next\r\n        Dhead = prev\r\n        return Dhead\r\n\r\n    # Function to print linked list\r\n    def printList(self):\r\n        temp = self.head\r\n        while temp != None:\r\n            print (temp.data,end=\" \")\r\n            temp = temp.next\r\n        print()\r\n\r\n    # A utility function to merge two sorted linked lists\r\n    def mergeList(self, head1, head2):\r\n        # Base cases\r\n        if head1 == None:\r\n            return head2\r\n        if head2 == None:\r\n            return head1\r\n        temp = None\r\n        if head1.data < head2.data:\r\n            temp = head1\r\n            head1.next = self.mergeList(head1.next, head2)\r\n        else:\r\n            temp = head2\r\n            head2.next = self.mergeList(head1, head2.next)\r\n        return temp\r\n\r\n    # This function alternatively splits a linked list with head\r\n    # as head into two:\r\n    # \"Ahead\" is reference to head of ascending linked list\r\n    # \"Dhead\" is reference to head of descending linked list\r\n    def splitList(self, Ahead, Dhead):\r\n        ascn = Ahead\r\n        dscn = Dhead\r\n        curr = self.head\r\n        # Link alternate nodes\r\n        while curr != None:\r\n            # Link alternate nodes in ascending order\r\n            ascn.next = curr\r\n            ascn = ascn.next\r\n            curr = curr.next\r\n            if curr != None:\r\n                dscn.next = curr\r\n                dscn = dscn.next\r\n                curr = curr.next\r\n        ascn.next = None\r\n        dscn.next = None\r\n\r\nllist = LinkedList()\r\nllist.head = llist.newNode(1)\r\nllist.head.next = llist.newNode(9)\r\nllist.head.next.next = llist.newNode(5)\r\nllist.head.next.next.next = llist.newNode(8)\r\nllist.head.next.next.next.next = llist.newNode(7)\r\n\r\nprint ('Given linked list')\r\nllist.printList()\r\n\r\nllist.sort()\r\n\r\nprint ('Sorted linked list')\r\nllist.printList()\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_4688 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_4688 a\"),jQuery(\"#tab-content_4688\"));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><strong>Input Linked list:<\/strong><br \/>\n1 9 5 8 7<br \/>\n<strong>Final Sorted Linked List:<\/strong><br \/>\n1 5 7 8 9<\/p>\n<p><strong>Time Complexity:<\/strong> O(n), We have traversed through the linked list only once to separate the list and reverse the list. The merging of two sorted lists into a single linked list takes O(n) time.<\/p>\n<p><strong>Auxiliary Space:<\/strong> O(1), No extra space is required.<\/p>\n<p>So, In this blog, we have learned to sort a linked list that is sorted alternating ascending and descending orders. If 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","protected":false},"excerpt":{"rendered":"<p>Introduction The linked list is one of the most important concepts and data structures to learn while preparing for interviews. Having a good grasp of Linked Lists can be a huge plus point in a coding interview. Problem Statement According to the problem statement, our task is to sort a linked list that is sorted [&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-4687","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>Sort a linked list that is sorted alternating ascending and descending orders<\/title>\n<meta name=\"description\" content=\"Learn the most efficient way to sort a linked list that is sorted alternating ascending and descending orders from this blog.\" \/>\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\/sort-a-linked-list-that-is-sorted-alternating-ascending-and-descending-orders\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Sort a linked list that is sorted alternating ascending and descending orders\" \/>\n<meta property=\"og:description\" content=\"Learn the most efficient way to sort a linked list that is sorted alternating ascending and descending orders from this blog.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/sort-a-linked-list-that-is-sorted-alternating-ascending-and-descending-orders\/\" \/>\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-02T09:49:12+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-03-10T18:24:27+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644926345566-114.Sort%20a%20linked%20list%20that%20is%20sorted%20alternating%20ascending%20and%20descending%20orders_Artboard%203.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=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/sort-a-linked-list-that-is-sorted-alternating-ascending-and-descending-orders\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/sort-a-linked-list-that-is-sorted-alternating-ascending-and-descending-orders\/\"},\"author\":{\"name\":\"PrepBytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e\"},\"headline\":\"Sort a linked list that is sorted alternating ascending and descending orders\",\"datePublished\":\"2021-09-02T09:49:12+00:00\",\"dateModified\":\"2022-03-10T18:24:27+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/sort-a-linked-list-that-is-sorted-alternating-ascending-and-descending-orders\/\"},\"wordCount\":625,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/sort-a-linked-list-that-is-sorted-alternating-ascending-and-descending-orders\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644926345566-114.Sort%20a%20linked%20list%20that%20is%20sorted%20alternating%20ascending%20and%20descending%20orders_Artboard%203.png\",\"articleSection\":[\"Linked list articles\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/sort-a-linked-list-that-is-sorted-alternating-ascending-and-descending-orders\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/sort-a-linked-list-that-is-sorted-alternating-ascending-and-descending-orders\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/sort-a-linked-list-that-is-sorted-alternating-ascending-and-descending-orders\/\",\"name\":\"Sort a linked list that is sorted alternating ascending and descending orders\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/sort-a-linked-list-that-is-sorted-alternating-ascending-and-descending-orders\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/sort-a-linked-list-that-is-sorted-alternating-ascending-and-descending-orders\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644926345566-114.Sort%20a%20linked%20list%20that%20is%20sorted%20alternating%20ascending%20and%20descending%20orders_Artboard%203.png\",\"datePublished\":\"2021-09-02T09:49:12+00:00\",\"dateModified\":\"2022-03-10T18:24:27+00:00\",\"description\":\"Learn the most efficient way to sort a linked list that is sorted alternating ascending and descending orders from this blog.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/sort-a-linked-list-that-is-sorted-alternating-ascending-and-descending-orders\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/sort-a-linked-list-that-is-sorted-alternating-ascending-and-descending-orders\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/sort-a-linked-list-that-is-sorted-alternating-ascending-and-descending-orders\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644926345566-114.Sort%20a%20linked%20list%20that%20is%20sorted%20alternating%20ascending%20and%20descending%20orders_Artboard%203.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644926345566-114.Sort%20a%20linked%20list%20that%20is%20sorted%20alternating%20ascending%20and%20descending%20orders_Artboard%203.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/sort-a-linked-list-that-is-sorted-alternating-ascending-and-descending-orders\/#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\":\"Sort a linked list that is sorted alternating ascending and descending orders\"}]},{\"@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":"Sort a linked list that is sorted alternating ascending and descending orders","description":"Learn the most efficient way to sort a linked list that is sorted alternating ascending and descending orders from this blog.","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\/sort-a-linked-list-that-is-sorted-alternating-ascending-and-descending-orders\/","og_locale":"en_US","og_type":"article","og_title":"Sort a linked list that is sorted alternating ascending and descending orders","og_description":"Learn the most efficient way to sort a linked list that is sorted alternating ascending and descending orders from this blog.","og_url":"https:\/\/prepbytes.com\/blog\/sort-a-linked-list-that-is-sorted-alternating-ascending-and-descending-orders\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2021-09-02T09:49:12+00:00","article_modified_time":"2022-03-10T18:24:27+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644926345566-114.Sort%20a%20linked%20list%20that%20is%20sorted%20alternating%20ascending%20and%20descending%20orders_Artboard%203.png","type":"","width":"","height":""}],"author":"PrepBytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"PrepBytes","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/sort-a-linked-list-that-is-sorted-alternating-ascending-and-descending-orders\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/sort-a-linked-list-that-is-sorted-alternating-ascending-and-descending-orders\/"},"author":{"name":"PrepBytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e"},"headline":"Sort a linked list that is sorted alternating ascending and descending orders","datePublished":"2021-09-02T09:49:12+00:00","dateModified":"2022-03-10T18:24:27+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/sort-a-linked-list-that-is-sorted-alternating-ascending-and-descending-orders\/"},"wordCount":625,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/sort-a-linked-list-that-is-sorted-alternating-ascending-and-descending-orders\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644926345566-114.Sort%20a%20linked%20list%20that%20is%20sorted%20alternating%20ascending%20and%20descending%20orders_Artboard%203.png","articleSection":["Linked list articles"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/sort-a-linked-list-that-is-sorted-alternating-ascending-and-descending-orders\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/sort-a-linked-list-that-is-sorted-alternating-ascending-and-descending-orders\/","url":"https:\/\/prepbytes.com\/blog\/sort-a-linked-list-that-is-sorted-alternating-ascending-and-descending-orders\/","name":"Sort a linked list that is sorted alternating ascending and descending orders","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/sort-a-linked-list-that-is-sorted-alternating-ascending-and-descending-orders\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/sort-a-linked-list-that-is-sorted-alternating-ascending-and-descending-orders\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644926345566-114.Sort%20a%20linked%20list%20that%20is%20sorted%20alternating%20ascending%20and%20descending%20orders_Artboard%203.png","datePublished":"2021-09-02T09:49:12+00:00","dateModified":"2022-03-10T18:24:27+00:00","description":"Learn the most efficient way to sort a linked list that is sorted alternating ascending and descending orders from this blog.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/sort-a-linked-list-that-is-sorted-alternating-ascending-and-descending-orders\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/sort-a-linked-list-that-is-sorted-alternating-ascending-and-descending-orders\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/sort-a-linked-list-that-is-sorted-alternating-ascending-and-descending-orders\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644926345566-114.Sort%20a%20linked%20list%20that%20is%20sorted%20alternating%20ascending%20and%20descending%20orders_Artboard%203.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644926345566-114.Sort%20a%20linked%20list%20that%20is%20sorted%20alternating%20ascending%20and%20descending%20orders_Artboard%203.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/sort-a-linked-list-that-is-sorted-alternating-ascending-and-descending-orders\/#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":"Sort a linked list that is sorted alternating ascending and descending orders"}]},{"@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\/4687","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=4687"}],"version-history":[{"count":5,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/4687\/revisions"}],"predecessor-version":[{"id":7942,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/4687\/revisions\/7942"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=4687"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=4687"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=4687"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}