{"id":4651,"date":"2021-09-01T11:53:03","date_gmt":"2021-09-01T11:53:03","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=4651"},"modified":"2022-11-28T10:59:47","modified_gmt":"2022-11-28T10:59:47","slug":"partitioning-a-linked-list-around-a-given-value-and-if-we-dont-care-about-making-the-elements-of-the-list-stable","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/partitioning-a-linked-list-around-a-given-value-and-if-we-dont-care-about-making-the-elements-of-the-list-stable\/","title":{"rendered":"Partitioning a linked list around a given value and If we don\u2019t care about making the elements of the list \u201cstable\u201d"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644922044898-87Partitioning_Artboard%206.png\" alt=\"\" \/><\/p>\n<p>This article will discuss the problem partition a linked list around a given value and its proper solution using an algorithm and dry run. Before jumping into the details of the problem and approach to solving it, you need to know about the problem statement of partition a linked list around a given value.<\/p>\n<h2>How to  partition a linked list around a given value <\/h2>\n<p>In this problem, we will be given a linked list and an integer <strong>X<\/strong>. We need to partition the linked list around this integer <strong>X<\/strong> such that all elements that are less than <strong>X<\/strong> should occur before all elements having a value greater than or equal to <strong>X<\/strong>.<\/p>\n<p>To understand this problem statement, let us learn programming languages online and take an example.<\/p>\n<p>If the given linked list is:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/input-1.png\" alt=\"\" \/><\/p>\n<p>and X = 5<\/p>\n<ul>\n<li>Here, <strong>X<\/strong> = 5 so, nodes with data less than <strong>X<\/strong> are: <strong>1,2,3<\/strong>.<\/li>\n<li>Nodes with data greater than or equal to <strong>X<\/strong> are: <strong>5,8,12<\/strong>.<\/li>\n<li>We need to keep <strong>1,2,3<\/strong> before <strong>5,8,12<\/strong>.<\/li>\n<li>Remember that the order of elements does not matter to us as long as the criteria are satisfied, i.e., elements having a value less than <strong>X<\/strong> should be before the elements with a value greater than or equal to <strong>X<\/strong>.<\/li>\n<li>So, the final output will be 1\u21922\u21923\u21925\u21928\u219212\u2192NULL<\/li>\n<\/ul>\n<p>Let us take another example:<br \/>\nIf the linked list is 9\u21921\u219210\u219227\u219242\u21922\u2192NULL and <strong>X<\/strong> = 10<\/p>\n<ul>\n<li>As explained in the above example, similarly, the output linked list after partitioning the linked list around <strong>X<\/strong> = 10 will be: 1\u21929\u21922\u219210\u219227\u219242\u2192NULL<\/li>\n<\/ul>\n<p><strong>Note:<\/strong> The order of the elements does not matter, i.e., in the final linked list, the elements smaller than <strong>X<\/strong> should be before the elements greater than or equal to <strong>X<\/strong>. The order of the occurrence of the elements in the final linked list need not be the same as the order of occurrence in the initial given linked list.  We can change the order of occurrence of elements as long as the main condition is satisfied.<\/p>\n<p>Also, multiple correct outputs are possible for this problem statement.<\/p>\n<p>Now let&#8217;s have a look at some helpful observations.<\/p>\n<p><strong>Helpful Observations<\/strong><\/p>\n<ul>\n<li>We need to separate the nodes with values less than <strong>X<\/strong> from those with values greater than or equal to <strong>X<\/strong>.<\/li>\n<li>The order of occurrence of nodes does not matter to us.<\/li>\n<li>Multiple correct solutions exist for this problem.<\/li>\n<\/ul>\n<h2>Approach to partition a linked list around a given value<\/h2>\n<ul>\n<li>Here, we will keep track of two pointers i.e., <strong>head<\/strong> and <strong>tail<\/strong> pointers of the list<\/li>\n<li>When we encounter an element that is less than <strong>X<\/strong>, we will insert it before the <strong>head<\/strong> and update the <strong>head<\/strong> pointer.<\/li>\n<li>When we encounter an element greater than or equal to <strong>X<\/strong>, we will insert it after the tail node and will update the tail node.<\/li>\n<\/ul>\n<h2>Algorithm to partition a linked list around a given value<\/h2>\n<ul>\n<li>Initialize two pointers named <strong>current<\/strong> and <strong>tail<\/strong> with the starting node of the original linked list.<\/li>\n<li>Loop in the linked list using this pointer <strong>current<\/strong>, starting from first to the last node, and store the next pointer of the <strong>current node<\/strong> in another variable before inserting the <strong>current node<\/strong> <strong>before head<\/strong> or <strong>after tail<\/strong>.<\/li>\n<li>If the current node has a value less than <strong>X<\/strong>, insert it before <strong>head<\/strong> and update the <strong>head<\/strong>.<\/li>\n<li>If the current node has a value greater than or equal to <strong>X<\/strong>, insert it after the <strong>tail<\/strong> and update the <strong>tail<\/strong> pointer.<\/li>\n<li>Update the <strong>current node<\/strong> with the <strong>next<\/strong> pointer stored in step 2.<\/li>\n<li>After the loop ends, make the <strong>next pointer of the tail node point to NULL<\/strong> to avoid the cycle in the newly created list.<\/li>\n<\/ul>\n<h3>Dry Run of partition a linked list around a given value<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/Partitioning-a-linked-list-around-a-given-value1.png\" alt=\"\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/Partitioning-a-linked-list-around-a-given-value-2.png\" alt=\"\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/Partitioning-a-linked-list-3.png\" alt=\"\" \/><\/p>\n<h2>Code Implementation to partition a linked list around a given value<\/h2>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_4652 {\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_4652 .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_4652 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_4652 .wpsm_nav-tabs > li.active > a, #tab_container_4652 .wpsm_nav-tabs > li.active > a:hover, #tab_container_4652 .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_4652 .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_4652 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_4652 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_4652 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_4652 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_4652 .wpsm_nav-tabs > li > a:hover , #tab_container_4652 .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_4652 .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_4652 .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_4652 .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_4652 .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_4652 .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_4652 .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_4652 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_4652 .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_4652 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_4652 .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_4652 .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_4652\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_4652\">\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_4652_1\" aria-controls=\"tabs_desc_4652_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_4652_2\" aria-controls=\"tabs_desc_4652_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_4652_3\" aria-controls=\"tabs_desc_4652_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_4652_4\" aria-controls=\"tabs_desc_4652_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_4652\">\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_4652_1\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"c\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include&lt;stdio.h&gt;\r\n#include&lt;stdlib.h&gt;\r\n\r\nstruct Node\r\n{\r\n    int data;\r\n    struct Node* next;\r\n};\r\n \r\n\/\/ A utility function to create a new node\r\nNode *newNode(int data)\r\n{\r\n    struct Node* new_node = new Node;\r\n    new_node-&gt;data  = data;\r\n    new_node-&gt;next = NULL;\r\n    return new_node;\r\n}\r\n \r\n\/\/ Function to make a new list(using the existing\r\n\/\/ nodes) and return head of new list.\r\nstruct Node *partition(struct Node *head, int x)\r\n{\r\n    \/* Let us initialize start and tail nodes of\r\n    new list *\/\r\n    struct Node *tail = head;\r\n \r\n    \/\/ Now iterate original list and connect nodes\r\n    Node *curr = head;\r\n    while (curr != NULL)\r\n    {\r\n        struct Node *next = curr-&gt;next;\r\n        if (curr-&gt;data &lt; x)\r\n        {\r\n            \/* Insert node at head. *\/\r\n            curr-&gt;next = head;\r\n            head = curr;\r\n        }\r\n \r\n        else \/\/ Append to the list of greater values\r\n        {\r\n            \/* Insert node at tail. *\/\r\n            tail-&gt;next = curr;\r\n            tail = curr;\r\n        }\r\n        curr = next;\r\n    }\r\n    tail-&gt;next = NULL;\r\n \r\n    \/\/ The head has changed, so we need\r\n    \/\/ to return it to the user.\r\n    return head;\r\n}\r\n \r\n\/* Function to print linked list *\/\r\nvoid printList(struct Node *head)\r\n{\r\n    struct Node *temp = head;\r\n    while (temp != NULL)\r\n    {\r\n        printf(&quot;%d  &quot;, temp-&gt;data);\r\n        temp = temp-&gt;next;\r\n    }\r\n}\r\n \r\n\/\/ Driver program to run the case\r\nint main()\r\n{\r\n    \/* Start with the empty list *\/\r\n    struct Node* head = newNode(3);\r\n    head-&gt;next = newNode(5);\r\n    head-&gt;next-&gt;next = newNode(8);\r\n    head-&gt;next-&gt;next-&gt;next = newNode(2);\r\n    head-&gt;next-&gt;next-&gt;next-&gt;next = newNode(10);\r\n    head-&gt;next-&gt;next-&gt;next-&gt;next-&gt;next = newNode(2);\r\n    head-&gt;next-&gt;next-&gt;next-&gt;next-&gt;next-&gt;next = newNode(1);\r\n \r\n    int x = 5;\r\n    head = partition(head, x);\r\n    printList(head);\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_4652_2\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"cpp\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include&lt;bits stdc++.h=&quot;&quot;&gt;\r\nusing namespace std;\r\nclass Node\r\n{\r\n    public:\r\n    int data;\r\n    Node* next;\r\n    Node(int x){\r\n        data = x;\r\n   next = NULL;\r\n    }\r\n};\r\n \r\n \r\nNode *partition(Node *head, int x)\r\n{\r\n    \/* initialize current and tail nodes of new list \r\nas discussed in step 1*\/\r\n    Node *tail = head;\r\n \r\n    Node *curr = head;\r\n    while (curr != NULL)\r\n    {\r\n        Node *next = curr-&gt;next;\r\n        if (curr-&gt;data &lt; x) \/\/ left partition \r\n        {\r\n            \/* Insert node before head if current node data is\r\n               less than given value of X *\/\r\n            curr-&gt;next = head;\r\n            head = curr; \/\/ update the head node \r\n        }\r\n \r\n        else \/\/ right partition \r\n        {\r\n            \/* Insert node after tail node *\/\r\n            tail-&gt;next = curr;\r\n            tail = curr; \/\/ update the tail node\r\n        }\r\n        curr = next;\r\n    }\r\n    tail-&gt;next = NULL; \/\/ make next of tail node as NULL to  \r\n                       \/\/ avoid cycles in newly created list\r\n \r\n    \/\/ return changed head\r\n    return head;\r\n}\r\n \r\nvoid printList(struct Node *head)\r\n{\r\n    Node *temp = head;\r\n    while (temp != NULL)\r\n    {\r\n        printf(&quot;%d  &quot;, temp-&gt;data);\r\n        temp = temp-&gt;next;\r\n    }\r\n}\r\n \r\n \r\nint main(void)\r\n{\r\n    Node* head = NULL;\r\n    head = new Node(3);\r\n    head-&gt;next = new Node(12);\r\n    head-&gt;next-&gt;next = new Node(1);\r\n    head-&gt;next-&gt;next-&gt;next = new Node(5);\r\n    head-&gt;next-&gt;next-&gt;next-&gt;next = new Node(8);\r\n    head-&gt;next-&gt;next-&gt;next-&gt;next-&gt;next = new Node(2);\r\n    \r\n    Node *tmp = partition(head,5);\r\n    printList(tmp);\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_4652_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 Partition \r\n{\r\n\r\n    static class Node\r\n    {\r\n        int data;\r\n        Node next;\r\n    }\r\n    static Node newNode(int data)\r\n    {\r\n        Node new_node = new Node();\r\n        new_node.data = data;\r\n        new_node.next = null;\r\n        return new_node;\r\n    }\r\n    \/\/ Function to make a new list\r\n    \/\/ (using the existing nodes) and\r\n    \/\/ return head of new list.\r\n    static Node partition(Node head, int x)\r\n    {\r\n        \/* Let us initialize start and tail nodes of\r\n        new list *\/\r\n        Node tail = head;\r\n    \r\n        \/\/ Now iterate original list and connect nodes\r\n        Node curr = head;\r\n        while (curr != null)\r\n        {\r\n            Node next = curr.next;\r\n            if (curr.data &lt; x)\r\n            {\r\n                \/* Insert node at head. *\/\r\n                curr.next = head;\r\n                head = curr;\r\n            }\r\n    \r\n            else \/\/ Append to the list of greater values\r\n            {\r\n                \/* Insert node at tail. *\/\r\n                tail.next = curr;\r\n                tail = curr;\r\n            }\r\n            curr = next;\r\n        }\r\n        tail.next = null;\r\n    \r\n        \/\/ The head has changed, so we need\r\n        \/\/ to return it to the user.\r\n        return head;\r\n    }\r\n    static void printList(Node head)\r\n    {\r\n        Node temp = head;\r\n        while (temp != null)\r\n        {\r\n            System.out.print(temp.data + &quot; &quot;);\r\n            temp = temp.next;\r\n        }\r\n    }\r\n    \/\/ Driver code\r\n    public static void main(String[] args)\r\n    {\r\n        \/* Start with the empty list *\/\r\n        Node head = newNode(3);\r\n        head.next = newNode(5);\r\n        head.next.next = newNode(8);\r\n        head.next.next.next = newNode(2);\r\n        head.next.next.next.next = newNode(10);\r\n        head.next.next.next.next.next = newNode(2);\r\n        head.next.next.next.next.next.next = newNode(1);\r\n    \r\n        int x = 5;\r\n        head = partition(head, x);\r\n        printList(head);\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_4652_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\nclass Node:\r\n    def __init__(self, data):\r\n        self.data = data\r\n        self.next = None\r\n\r\ndef newNode(data):\r\n    new_node = Node(data)\r\n    new_node.data = data\r\n    new_node.next = None\r\n    return new_node\r\n\r\n# Function to make a new list\r\n# (using the existing nodes)\r\n# and return head of new list.\r\ndef partition(head, x):\r\n    \r\n    # Let us initialize start and\r\n    # tail nodes of new list\r\n    tail = head\r\n\r\n    # Now iterate original list\r\n    # and connect nodes\r\n    curr = head\r\n    while (curr != None):\r\n        next = curr.next\r\n        if (curr.data &lt; x):\r\n            \r\n            # Insert node at head.\r\n            curr.next = head\r\n            head = curr\r\n        \r\n        else:\r\n            \r\n            # Append to the list of greater values\r\n            # Insert node at tail.\r\n            tail.next = curr\r\n            tail = curr\r\n        \r\n        curr = next\r\n    \r\n    tail.next = None\r\n\r\n    # The head has changed, so we need\r\n    # to return it to the user.\r\n    return head\r\n\r\n# Function to print linked list\r\ndef printList(head):\r\n    temp = head\r\n    while (temp != None):\r\n        print(temp.data, end = &quot; &quot;)\r\n        temp = temp.next\r\n    \r\n# Driver Code\r\nif __name__=='__main__':\r\n    \r\n    # Start with the empty list\r\n    head = newNode(3)\r\n    head.next = newNode(12)\r\n    head.next.next = newNode(1)\r\n    head.next.next.next = newNode(5)\r\n    head.next.next.next.next = newNode(8)\r\n    head.next.next.next.next.next = newNode(2)\r\n\r\n    x = 5\r\n    head = partition(head, x)\r\n    printList(head)\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_4652 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_4652 a\"),jQuery(\"#tab-content_4652\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<pre><code>Output\n2 1 3 12 5 8<\/code><\/pre>\n<p><strong>Time Complexity:<\/strong> O(n), Where <strong>n<\/strong> is the number of nodes in the list.<\/p>\n<p>This article discusses how you can partition a linked list around a given value without caring about maintaining the stability in elements of the list in the most optimal way. We have discussed the most-efficient approach to solve the problem, and discussed the time and space complexities. If you want to solve more questions on Linked List, which is 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 related to partition a linked list around a given value<\/h2>\n<p><strong>1. What is a linked list?<\/strong><br \/>\nA linked list is a linear data structure that is formed by a collection of connected nodes. Each node consists of data and a pointer to the next node.<\/p>\n<p><strong>2. State the main difference between a singly linked list and a doubly linked list?<\/strong><br \/>\nA singly-linked list is unidirectional, which means that the node of a singly-linked list contains the pointer to its next node only. In contrast, a doubly-linked list is bidirectional, and its node contains the pointer to its next and previous nodes.<\/p>\n<p><strong>3. How do you split a linked list?<\/strong><\/p>\n<ul>\n<li>Store the mid and last pointers of the circular linked list using the tortoise and hare algorithm.<\/li>\n<li>Make the second half circular.<\/li>\n<li>Make the first half circular.<\/li>\n<li>Set head (or start) pointers of the two linked lists.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>This article will discuss the problem partition a linked list around a given value and its proper solution using an algorithm and dry run. Before jumping into the details of the problem and approach to solving it, you need to know about the problem statement of partition a linked list around a given value. How [&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-4651","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>Partitioning a linked list around a given value<\/title>\n<meta name=\"description\" content=\"Learn how to partition a linked list around a given value, and we don\u2019t care about making elements of the list stable.\" \/>\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\/partitioning-a-linked-list-around-a-given-value-and-if-we-dont-care-about-making-the-elements-of-the-list-stable\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Partitioning a linked list around a given value\" \/>\n<meta property=\"og:description\" content=\"Learn how to partition a linked list around a given value, and we don\u2019t care about making elements of the list stable.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/partitioning-a-linked-list-around-a-given-value-and-if-we-dont-care-about-making-the-elements-of-the-list-stable\/\" \/>\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-01T11:53:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-28T10:59:47+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644922044898-87Partitioning_Artboard%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=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/partitioning-a-linked-list-around-a-given-value-and-if-we-dont-care-about-making-the-elements-of-the-list-stable\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/partitioning-a-linked-list-around-a-given-value-and-if-we-dont-care-about-making-the-elements-of-the-list-stable\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Partitioning a linked list around a given value and If we don\u2019t care about making the elements of the list \u201cstable\u201d\",\"datePublished\":\"2021-09-01T11:53:03+00:00\",\"dateModified\":\"2022-11-28T10:59:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/partitioning-a-linked-list-around-a-given-value-and-if-we-dont-care-about-making-the-elements-of-the-list-stable\/\"},\"wordCount\":873,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/partitioning-a-linked-list-around-a-given-value-and-if-we-dont-care-about-making-the-elements-of-the-list-stable\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644922044898-87Partitioning_Artboard%206.png\",\"articleSection\":[\"Linked list articles\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/partitioning-a-linked-list-around-a-given-value-and-if-we-dont-care-about-making-the-elements-of-the-list-stable\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/partitioning-a-linked-list-around-a-given-value-and-if-we-dont-care-about-making-the-elements-of-the-list-stable\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/partitioning-a-linked-list-around-a-given-value-and-if-we-dont-care-about-making-the-elements-of-the-list-stable\/\",\"name\":\"Partitioning a linked list around a given value\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/partitioning-a-linked-list-around-a-given-value-and-if-we-dont-care-about-making-the-elements-of-the-list-stable\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/partitioning-a-linked-list-around-a-given-value-and-if-we-dont-care-about-making-the-elements-of-the-list-stable\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644922044898-87Partitioning_Artboard%206.png\",\"datePublished\":\"2021-09-01T11:53:03+00:00\",\"dateModified\":\"2022-11-28T10:59:47+00:00\",\"description\":\"Learn how to partition a linked list around a given value, and we don\u2019t care about making elements of the list stable.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/partitioning-a-linked-list-around-a-given-value-and-if-we-dont-care-about-making-the-elements-of-the-list-stable\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/partitioning-a-linked-list-around-a-given-value-and-if-we-dont-care-about-making-the-elements-of-the-list-stable\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/partitioning-a-linked-list-around-a-given-value-and-if-we-dont-care-about-making-the-elements-of-the-list-stable\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644922044898-87Partitioning_Artboard%206.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644922044898-87Partitioning_Artboard%206.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/partitioning-a-linked-list-around-a-given-value-and-if-we-dont-care-about-making-the-elements-of-the-list-stable\/#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\":\"Partitioning a linked list around a given value and If we don\u2019t care about making the elements of the list \u201cstable\u201d\"}]},{\"@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":"Partitioning a linked list around a given value","description":"Learn how to partition a linked list around a given value, and we don\u2019t care about making elements of the list stable.","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\/partitioning-a-linked-list-around-a-given-value-and-if-we-dont-care-about-making-the-elements-of-the-list-stable\/","og_locale":"en_US","og_type":"article","og_title":"Partitioning a linked list around a given value","og_description":"Learn how to partition a linked list around a given value, and we don\u2019t care about making elements of the list stable.","og_url":"https:\/\/prepbytes.com\/blog\/partitioning-a-linked-list-around-a-given-value-and-if-we-dont-care-about-making-the-elements-of-the-list-stable\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2021-09-01T11:53:03+00:00","article_modified_time":"2022-11-28T10:59:47+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644922044898-87Partitioning_Artboard%206.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\/partitioning-a-linked-list-around-a-given-value-and-if-we-dont-care-about-making-the-elements-of-the-list-stable\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/partitioning-a-linked-list-around-a-given-value-and-if-we-dont-care-about-making-the-elements-of-the-list-stable\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Partitioning a linked list around a given value and If we don\u2019t care about making the elements of the list \u201cstable\u201d","datePublished":"2021-09-01T11:53:03+00:00","dateModified":"2022-11-28T10:59:47+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/partitioning-a-linked-list-around-a-given-value-and-if-we-dont-care-about-making-the-elements-of-the-list-stable\/"},"wordCount":873,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/partitioning-a-linked-list-around-a-given-value-and-if-we-dont-care-about-making-the-elements-of-the-list-stable\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644922044898-87Partitioning_Artboard%206.png","articleSection":["Linked list articles"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/partitioning-a-linked-list-around-a-given-value-and-if-we-dont-care-about-making-the-elements-of-the-list-stable\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/partitioning-a-linked-list-around-a-given-value-and-if-we-dont-care-about-making-the-elements-of-the-list-stable\/","url":"https:\/\/prepbytes.com\/blog\/partitioning-a-linked-list-around-a-given-value-and-if-we-dont-care-about-making-the-elements-of-the-list-stable\/","name":"Partitioning a linked list around a given value","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/partitioning-a-linked-list-around-a-given-value-and-if-we-dont-care-about-making-the-elements-of-the-list-stable\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/partitioning-a-linked-list-around-a-given-value-and-if-we-dont-care-about-making-the-elements-of-the-list-stable\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644922044898-87Partitioning_Artboard%206.png","datePublished":"2021-09-01T11:53:03+00:00","dateModified":"2022-11-28T10:59:47+00:00","description":"Learn how to partition a linked list around a given value, and we don\u2019t care about making elements of the list stable.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/partitioning-a-linked-list-around-a-given-value-and-if-we-dont-care-about-making-the-elements-of-the-list-stable\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/partitioning-a-linked-list-around-a-given-value-and-if-we-dont-care-about-making-the-elements-of-the-list-stable\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/partitioning-a-linked-list-around-a-given-value-and-if-we-dont-care-about-making-the-elements-of-the-list-stable\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644922044898-87Partitioning_Artboard%206.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644922044898-87Partitioning_Artboard%206.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/partitioning-a-linked-list-around-a-given-value-and-if-we-dont-care-about-making-the-elements-of-the-list-stable\/#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":"Partitioning a linked list around a given value and If we don\u2019t care about making the elements of the list \u201cstable\u201d"}]},{"@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\/4651","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=4651"}],"version-history":[{"count":7,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/4651\/revisions"}],"predecessor-version":[{"id":10793,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/4651\/revisions\/10793"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=4651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=4651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=4651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}