{"id":784,"date":"2017-11-13T19:34:58","date_gmt":"2017-11-14T03:34:58","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=784"},"modified":"2018-04-19T08:34:30","modified_gmt":"2018-04-19T15:34:30","slug":"leetcode-725-split-linked-list-in-parts","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/list\/leetcode-725-split-linked-list-in-parts\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 725. Split Linked List in Parts"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 725. Split Linked List in Parts - \u5237\u9898\u627e\u5de5\u4f5c EP106\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/fk8JTWhM-4U?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p><strong>Problem:<\/strong><\/p>\n<div class=\"question-description\">\n<p>Given a (singly) linked list with head node\u00a0<code>root<\/code>, write a function to split the linked list into\u00a0<code>k<\/code>\u00a0consecutive linked list &#8220;parts&#8221;.<\/p>\n<p>The length of each part should be as equal as possible: no two parts should have a size differing by more than 1. This may lead to some parts being null.<\/p>\n<p>The parts should be in order of occurrence in the input list, and parts occurring earlier should always have a size greater than or equal parts occurring later.<\/p>\n<p>Return a List of ListNode&#8217;s representing the linked list parts that are formed.<\/p>\n<p>Examples 1-&gt;2-&gt;3-&gt;4, k = 5 \/\/ 5 equal parts [ [1], [2], [3], [4], null ]<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"\">Input: \r\nroot = [1, 2, 3], k = 5\r\nOutput: [[1],[2],[3],[],[]]\r\nExplanation:\r\nThe input and each element of the output are ListNodes, not arrays.\r\nFor example, the input root has root.val = 1, root.next.val = 2, \\root.next.next.val = 3, and root.next.next.next = null.\r\nThe first element output[0] has output[0].val = 1, output[0].next = null.\r\nThe last element output[4] is null, but it's string representation as a ListNode is [].\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"\">Input: \r\nroot = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 3\r\nOutput: [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10]]\r\nExplanation:\r\nThe input has been split into consecutive parts with size difference at most 1, and earlier parts are a larger size than the later parts.\r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<ul>\n<li>The length of\u00a0<code>root<\/code>\u00a0will be in the range\u00a0<code>[0, 1000]<\/code>.<\/li>\n<li>Each value of a node in the input will be an integer in the range\u00a0<code>[0, 999]<\/code>.<\/li>\n<li><code>k<\/code>\u00a0will be an integer in the range\u00a0<code>[1, 50]<\/code>.<\/li>\n<\/ul>\n<\/div>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-2404451723245401\" data-ad-slot=\"7983117522\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<div><strong>Idea:<\/strong><\/div>\n<div><\/div>\n<div>List + Simulation<\/div>\n<div><\/div>\n<div><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/725-ep106-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-792\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/725-ep106-1.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/725-ep106-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/725-ep106-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/725-ep106-1-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/725-ep106-1-624x351.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/div>\n<div id=\"interviewed-div\"><\/div>\n<div><strong>Solution:<\/strong><\/div>\n<div><\/div>\n<div>C++<\/div>\n<div>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Runtime: 9 ms\r\nclass Solution {\r\npublic:\r\n    vector&lt;ListNode*&gt; splitListToParts(ListNode* root, int k) {\r\n        int len = 0;\r\n        for (ListNode* head = root; head; head = head-&gt;next) ++len;\r\n        vector&lt;ListNode*&gt; ans(k, nullptr);\r\n        int l = len \/ k;\r\n        int r = len % k;        \r\n        ListNode* head = root;\r\n        ListNode* prev = nullptr;\r\n        for (int i = 0; i &lt; k; ++i, --r) {\r\n            ans[i] = head;\r\n            for (int j = 0; j &lt; l + (r &gt; 0); ++j) {\r\n                prev = head;\r\n                head = head-&gt;next;\r\n            }\r\n            if (prev) prev-&gt;next = nullptr;\r\n        }\r\n        return ans;\r\n    }\r\n};<\/pre>\n<\/div>\n<p>Java<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Runtime: 4 ms\r\nclass Solution {\r\n    public ListNode[] splitListToParts(ListNode root, int k) {\r\n        ListNode[] ans = new ListNode[k];\r\n        int len = 0;\r\n        for (ListNode head = root; head != null; head = head.next) ++len;\r\n        int l = len \/ k;\r\n        int r = len % k;\r\n        ListNode prev = null;   \r\n        ListNode head = root;\r\n        for (int i = 0; i &lt; k; ++i, --r) {\r\n            ans[i] = head;\r\n            int part_len = l + ((r &gt; 0) ? 1 : 0);\r\n            for (int j = 0; j &lt; part_len; ++j) {\r\n                prev = head;\r\n                head = head.next;\r\n            }            \r\n            if (prev != null) prev.next = null;\r\n        }\r\n        return ans;\r\n    }\r\n}<\/pre>\n<p>Python<\/p>\n<pre class=\"lang:python decode:true \">\"\"\"\r\nAuthor: Huahua\r\nRuntime: 79 ms\r\n\"\"\"\r\nclass Solution:\r\n    def splitListToParts(self, root, k):\r\n        total_len = 0\r\n        head = root\r\n        while head:\r\n            total_len += 1\r\n            head = head.next\r\n        \r\n        ans = [None for _ in range(k)]\r\n        \r\n        l, r = total_len \/\/ k, total_len % k\r\n        \r\n        prev, head = None, root\r\n        \r\n        for i in range(k):\r\n            ans[i] = head\r\n            for j in range(l + (1 if r &gt; 0 else 0)):\r\n                prev, head = head, head.next\r\n            if prev: prev.next = None\r\n            r -= 1\r\n        \r\n        return ans<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: Given a (singly) linked list with head node\u00a0root, write a function to split the linked list into\u00a0k\u00a0consecutive linked list &#8220;parts&#8221;. The length of each&#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,164],"tags":[151,83,150],"class_list":["post-784","post","type-post","status-publish","format-standard","hentry","category-list","category-medium","tag-head","tag-list","tag-partition","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/784","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=784"}],"version-history":[{"count":9,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/784\/revisions"}],"predecessor-version":[{"id":2612,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/784\/revisions\/2612"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=784"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=784"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=784"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}