{"id":657,"date":"2020-09-26T11:07:58","date_gmt":"2020-09-26T03:07:58","guid":{"rendered":"http:\/\/39.96.58.60\/?p=657"},"modified":"2022-10-18T16:38:24","modified_gmt":"2022-10-18T08:38:24","slug":"leetcode-61-%e6%97%8b%e8%bd%ac%e9%93%be%e8%a1%a8","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=657","title":{"rendered":"Leetcode 61. \u65cb\u8f6c\u94fe\u8868"},"content":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u94fe\u8868\uff0c\u65cb\u8f6c\u94fe\u8868\uff0c\u5c06\u94fe\u8868\u6bcf\u4e2a\u8282\u70b9\u5411\u53f3\u79fb\u52a8&nbsp;<em>k&nbsp;<\/em>\u4e2a\u4f4d\u7f6e\uff0c\u5176\u4e2d&nbsp;<em>k&nbsp;<\/em>\u662f\u975e\u8d1f\u6570\u3002<\/p>\n<p><strong>\u793a\u4f8b&nbsp;1:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;NULL, k = 2\n\n<strong>\u8f93\u51fa:<\/strong> 4-&gt;5-&gt;1-&gt;2-&gt;3-&gt;NULL\n\n<strong>\u89e3\u91ca:<\/strong>\n\n\u5411\u53f3\u65cb\u8f6c 1 \u6b65: 5-&gt;1-&gt;2-&gt;3-&gt;4-&gt;NULL\n\n\u5411\u53f3\u65cb\u8f6c 2 \u6b65: 4-&gt;5-&gt;1-&gt;2-&gt;3-&gt;NULL\n\n<\/pre>\n<p><strong>\u793a\u4f8b&nbsp;2:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> 0-&gt;1-&gt;2-&gt;NULL, k = 4\n\n<strong>\u8f93\u51fa:<\/strong> <code>2-&gt;0-&gt;1-&gt;NULL<\/code>\n\n<strong>\u89e3\u91ca:<\/strong>\n\n\u5411\u53f3\u65cb\u8f6c 1 \u6b65: 2-&gt;0-&gt;1-&gt;NULL\n\n\u5411\u53f3\u65cb\u8f6c 2 \u6b65: 1-&gt;2-&gt;0-&gt;NULL\n\n\u5411\u53f3\u65cb\u8f6c 3 \u6b65:&nbsp;<code>0-&gt;1-&gt;2-&gt;NULL<\/code>\n\n\u5411\u53f3\u65cb\u8f6c 4 \u6b65:&nbsp;<code>2-&gt;0-&gt;1-&gt;NULL<\/code><\/pre>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u94fe\u8868\u3001 \u53cc\u6307\u9488\u3001<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-python\" data-lang=\"Python\"><code>\n# -*- coding: utf-8 -*-\n# @Author  : LG\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a36 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8698.33% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.1 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8698.58% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u53cc\u6307\u9488\uff0c\n    \u5c06p\u6307\u9488\u79fb\u52a8k\u6b65\uff0c\u7136\u540e\u79fb\u52a8q\u6307\u9488\uff0c\u627e\u5230\u79fb\u52a8\u540e\u7684\u94fe\u8868\u9996\u4f4d\u3002\u6b64\u65f6\uff0cq\u7684next\u6307\u5411\u79fb\u52a8\u540e\u7684\u94fe\u8868\u9996\u4f4d\uff0cp\u6307\u9488\u6307\u5411\u539f\u94fe\u8868\u672b\u5c3e\n    \u9700\u6ce8\u610f\u5927\u4e8e\u94fe\u8868\u957f\u5ea6\u7684\u79fb\u52a8\n\"\"\"\nclass Solution:\n    def rotateRight(self, head: ListNode, k: int) -&gt; ListNode:\n        if not head:    # \u94fe\u8868\u4e3a\u7a7a\uff0c\u76f4\u63a5\u8fd4\u56de\n            return head\n        if k == 0:  # \u79fb\u52a80\u6b65\uff0c\u76f4\u63a5\u8fd4\u56de\n            return head\n        p, q = head, head   # \u53cc\u6307\u9488\n        repeat = False  # \u662f\u5426\u8d85\u51fa\u4e86\u94fe\u8868\u957f\u5ea6\n        for i in range(k):  # \u5faa\u73afk\u6b65\n            if p.next:  # \u79fb\u52a8p\u6307\u9488\n                p = p.next\n            else:   # \u8d85\u51fa\u94fe\u8868\u957f\u5ea6\uff0c\u91cd\u65b0\u8ba1\u7b97\u79fb\u52a8\u7684\u6b65\u6570\n                k = k % (i+1)\n                repeat = True\n                break\n        if repeat:  # \u8d85\u51fa\u94fe\u8868\u957f\u5ea6\uff0c\u91cd\u65b0\u8ba1\u7b97\n            return self.rotateRight(head, k)\n\n        while p.next:   # \u79fb\u52a8p, q\u6307\u9488\uff0c\u76f4\u5230p\u6307\u9488\u6307\u5411\u539f\u94fe\u8868\u672b\u5c3e\u3002\u6b64\u65f6q\u6307\u9488next\u6307\u5411\u79fb\u52a8\u540e\u7684\u94fe\u8868\u9996\u4f4d\n            p = p.next\n            q = q.next\n        result = q.next # \u7ec4\u5408\u65b0\u94fe\u8868\n        p.next = head\n        q.next = None\n        return result<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u94fe\u8868\uff0c\u65cb\u8f6c\u94fe\u8868\uff0c\u5c06\u94fe\u8868\u6bcf\u4e2a\u8282\u70b9\u5411\u53f3\u79fb\u52a8&nbsp;k&nbsp;\u4e2a\u4f4d\u7f6e\uff0c\u5176\u4e2d&nbsp;k&nbsp;&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/657"}],"collection":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=657"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/657\/revisions"}],"predecessor-version":[{"id":658,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/657\/revisions\/658"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=657"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=657"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=657"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}