{"id":756,"date":"2025-10-26T20:47:21","date_gmt":"2025-10-26T12:47:21","guid":{"rendered":"http:\/\/www.guanhaobo.cn\/?p=756"},"modified":"2025-10-26T20:47:21","modified_gmt":"2025-10-26T12:47:21","slug":"leetcode-25-k-%e4%b8%aa%e4%b8%80%e7%bb%84%e7%bf%bb%e8%bd%ac%e9%93%be%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=756","title":{"rendered":"LeetCode 25 \u2014 K \u4e2a\u4e00\u7ec4\u7ffb\u8f6c\u94fe\u8868"},"content":{"rendered":"<h1>\u9898\u76ee\u63cf\u8ff0<\/h1>\n<p>\u7ed9\u4f60\u4e00\u4e2a\u94fe\u8868\uff0c\u6bcf\u00a0k\u00a0\u4e2a\u8282\u70b9\u4e00\u7ec4\u8fdb\u884c\u7ffb\u8f6c\uff0c\u8bf7\u4f60\u8fd4\u56de\u7ffb\u8f6c\u540e\u7684\u94fe\u8868\u3002<br \/>\nk\u00a0\u662f\u4e00\u4e2a\u6b63\u6574\u6570\uff0c\u5b83\u7684\u503c\u5c0f\u4e8e\u6216\u7b49\u4e8e\u94fe\u8868\u7684\u957f\u5ea6\u3002<br \/>\n\u5982\u679c\u8282\u70b9\u603b\u6570\u4e0d\u662f\u00a0k\u00a0\u7684\u6574\u6570\u500d\uff0c\u90a3\u4e48\u8bf7\u5c06\u6700\u540e\u5269\u4f59\u7684\u8282\u70b9\u4fdd\u6301\u539f\u6709\u987a\u5e8f\u3002<\/p>\n<p>\u793a\u4f8b\uff1a<br \/>\n\u7ed9\u4f60\u8fd9\u4e2a\u94fe\u8868\uff1a1->2->3->4->5<br \/>\n\u5f53\u00a0k\u00a0= 2 \u65f6\uff0c\u5e94\u5f53\u8fd4\u56de: 2->1->4->3->5<br \/>\n\u5f53\u00a0k\u00a0= 3 \u65f6\uff0c\u5e94\u5f53\u8fd4\u56de: 3->2->1->4->5<\/p>\n<p>\u8bf4\u660e\uff1a<br \/>\n\u4f60\u7684\u7b97\u6cd5\u53ea\u80fd\u4f7f\u7528\u5e38\u6570\u7684\u989d\u5916\u7a7a\u95f4\u3002<br \/>\n\u4f60\u4e0d\u80fd\u53ea\u662f\u5355\u7eaf\u7684\u6539\u53d8\u8282\u70b9\u5185\u90e8\u7684\u503c\uff0c\u800c\u662f\u9700\u8981\u5b9e\u9645\u8fdb\u884c\u8282\u70b9\u4ea4\u6362\u3002<\/p>\n<h1>\u9898\u76ee\u5206\u6790<\/h1>\n<p>\u6307\u9488\u6307\u5411\u5934\u90e8\uff0c\u5224\u65ad\u5269\u4f59\u957f\u5ea6\u662f\u5426\u8fbe\u5230 k\u3002<br \/>\n\u5982\u679c\u957f\u5ea6\u8db3\u591f\uff0c\u5219\u53ea\u5bf9\u524d k \u4e2a\u4f4d\u7f6e\u505a\u53cd\u8f6c\uff0c\u53cd\u8f6c\u540e\u628a\u5269\u4f59\u90e8\u5206\u7684\u8fde\u63a5\u505a\u597d\u3002\u540e\u7eed\u5c40\u90e8\u7684\u5934\u548c\u5c3e\u4f1a\u53d1\u751f\u4e92\u6362\uff0c\u5c06\u6307\u9488\u6307\u5411\u65b0\u7684\u5c40\u90e8\u5c3e\u3002<br \/>\n\u4e0d\u65ad\u91cd\u590d\uff0c\u76f4\u5230\u5269\u4f59\u90e8\u5206\u4e0d\u8db3k\u4e2a\u3002<\/p>\n<h1>Java<\/h1>\n<pre><code class=\"language-java line-numbers\">public ListNode reverseKGroup(ListNode head, int k) {\n    ListNode fake = new ListNode(0, head);\n    ListNode cur = fake;\n    while (length(cur.next) &gt;= k) {\n        ListNode pHead = reverseFirstK(cur.next, k);\n        ListNode pTail = cur.next;\n        cur.next = pHead;\n        cur = pTail;\n    }\n\n    return fake.next;\n}\n\nprivate int length(ListNode head) {\n    int length = 0;\n    while (head != null) {\n        head = head.next;\n        length++;\n    }\n    return length;\n}\n\nprivate ListNode reverseFirstK(ListNode head, int k) {\n    ListNode pre = null, cur = head;\n    while (cur != null &amp;&amp; k &gt; 0) {\n        ListNode temp = cur.next;\n        cur.next = pre;\n        pre = cur;\n        cur = temp;\n        k--;\n    }\n    head.next = cur;\n    return pre;\n}\n<\/code><\/pre>\n<h1>Kotlin<\/h1>\n<pre><code class=\"language-kotlin line-numbers\">fun reverseKGroup(head: ListNode?, k: Int): ListNode? {\n    val fake = ListNode(0)\n    fake.next = head\n    var cur: ListNode? = fake\n    while (length(cur?.next) &gt;= k) {\n        val pTail = cur?.next\n        val pHead = reverseFirstK(cur?.next, k)\n        cur?.next = pHead\n        cur = pTail\n    }\n\n    return fake.next\n}\n\nprivate fun length(head: ListNode?): Int {\n    var length = 0\n    var cur = head\n    while (cur != null) {\n        cur = cur.next\n        length++\n    }\n    return length\n}\n\nprivate fun reverseFirstK(head: ListNode?, k: Int): ListNode? {\n    var tail: ListNode? = null\n    var cur: ListNode? = head\n    var count = k\n    while (cur != null &amp;&amp; count &gt; 0) {\n        val temp = cur.next\n        cur.next = tail\n        tail = cur\n        cur = temp\n        count--\n    }\n    head?.next = cur\n    return tail\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0 \u7ed9\u4f60\u4e00\u4e2a\u94fe\u8868\uff0c\u6bcf\u00a0k\u00a0\u4e2a\u8282\u70b9\u4e00\u7ec4\u8fdb\u884c\u7ffb\u8f6c\uff0c\u8bf7\u4f60\u8fd4\u56de\u7ffb\u8f6c\u540e\u7684\u94fe\u8868\u3002 k\u00a0\u662f\u4e00\u4e2a\u6b63\u6574\u6570\uff0c\u5b83\u7684\u503c\u5c0f\u4e8e\u6216\u7b49\u4e8e [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[20,54,84],"class_list":["post-756","post","type-post","status-publish","format-standard","hentry","category-algo","tag-leetcode","tag-54","tag-84"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/756","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=756"}],"version-history":[{"count":1,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/756\/revisions"}],"predecessor-version":[{"id":918,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/756\/revisions\/918"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=756"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=756"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=756"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}