{"id":5657,"date":"2019-09-30T09:28:18","date_gmt":"2019-09-30T16:28:18","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5657"},"modified":"2019-09-30T09:29:42","modified_gmt":"2019-09-30T16:29:42","slug":"leetcode-86-partition-list","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/list\/leetcode-86-partition-list\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 86. Partition List"},"content":{"rendered":"\n<p>Given a linked list and a value&nbsp;<em>x<\/em>, partition it such that all nodes less than&nbsp;<em>x<\/em>&nbsp;come before nodes greater than or equal to&nbsp;<em>x<\/em>.<\/p>\n\n\n\n<p>You should preserve the original relative order of the nodes in each of the two partitions.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> head = 1-&gt;4-&gt;3-&gt;2-&gt;5-&gt;2, <em>x<\/em> = 3\n<strong>Output:<\/strong> 1-&gt;2-&gt;2-&gt;4-&gt;3-&gt;5<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Two dummy heads<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  ListNode* partition(ListNode* head, int x) {\n    ListNode dl(0);\n    ListNode dr(0);\n    ListNode* l = &dl;\n    ListNode* r = &dr;\n    while (head) {\n      ListNode*& h = (head->val < x) ? l : r;\n      h = h->next = head;\n      head = head->next;\n    }\n    r->next = nullptr; \/\/ important, to avoid loop\n    l->next = dr.next;\n    return dl.next;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a linked list and a value&nbsp;x, partition it such that all nodes less than&nbsp;x&nbsp;come before nodes greater than or equal to&nbsp;x. You should preserve&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[50],"tags":[83,177,150],"class_list":["post-5657","post","type-post","status-publish","format-standard","hentry","category-list","tag-list","tag-medium","tag-partition","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5657","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5657"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5657\/revisions"}],"predecessor-version":[{"id":5660,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5657\/revisions\/5660"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5657"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5657"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5657"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}