{"id":2434,"date":"2022-01-27T09:39:20","date_gmt":"2022-01-27T01:39:20","guid":{"rendered":"http:\/\/139.9.1.231\/?p=2434"},"modified":"2022-01-27T09:39:22","modified_gmt":"2022-01-27T01:39:22","slug":"leetcodeday92","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/01\/27\/leetcodeday92\/","title":{"rendered":"leetcodeday92 &#8211;\u53cd\u8f6c\u94fe\u8868"},"content":{"rendered":"\n<p>\u7ed9\u4f60\u5355\u94fe\u8868\u7684\u5934\u6307\u9488&nbsp;<code>head<\/code>&nbsp;\u548c\u4e24\u4e2a\u6574\u6570&nbsp;<code>left<\/code>&nbsp;\u548c&nbsp;<code>right<\/code>&nbsp;\uff0c\u5176\u4e2d&nbsp;<code>left &lt;= right<\/code>&nbsp;\u3002\u8bf7\u4f60\u53cd\u8f6c\u4ece\u4f4d\u7f6e&nbsp;<code>left<\/code>&nbsp;\u5230\u4f4d\u7f6e&nbsp;<code>right<\/code>&nbsp;\u7684\u94fe\u8868\u8282\u70b9\uff0c\u8fd4\u56de&nbsp;<strong>\u53cd\u8f6c\u540e\u7684\u94fe\u8868<\/strong>&nbsp;\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image is-style-default\"><img src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/02\/19\/rev2ex2.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>head = [1,2,3,4,5], left = 2, right = 4\n<strong>\u8f93\u51fa\uff1a<\/strong>[1,4,3,2,5]\n<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>head = [5], left = 1, right = 1\n<strong>\u8f93\u51fa\uff1a<\/strong>[5]<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code># &#91;92] \u53cd\u8f6c\u94fe\u8868 II\r\n#\u53cd\u8f6c left \u5230 right \u90e8\u5206\u4ee5\u540e\uff0c\u518d\u62fc\u63a5\u8d77\u6765 \r\n\r\n# @lc code=start\r\n# Definition for singly-linked list.\r\n# class ListNode:\r\n#     def __init__(self, val=0, next=None):\r\n#         self.val = val\r\n#         self.next = next\r\nclass Solution:\r\n    def reverseBetween(self, head: ListNode, left: int, right: int) -> ListNode:\r\n        def reverse_linked_list(head: ListNode):\r\n            # \u4e5f\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u53cd\u8f6c\u4e00\u4e2a\u94fe\u8868\r\n            pre = None\r\n            cur = head\r\n            while cur:\r\n                next = cur.next\r\n                cur.next = pre\r\n                pre = cur\r\n                cur = next\r\n\r\n        # \u56e0\u4e3a\u5934\u8282\u70b9\u6709\u53ef\u80fd\u53d1\u751f\u53d8\u5316\uff0c\u4f7f\u7528\u865a\u62df\u5934\u8282\u70b9\u53ef\u4ee5\u907f\u514d\u590d\u6742\u7684\u5206\u7c7b\u8ba8\u8bba\r\n        dummy_node = ListNode(-1)\r\n        dummy_node.next = head\r\n        pre = dummy_node\r\n        # \u7b2c 1 \u6b65\uff1a\u4ece\u865a\u62df\u5934\u8282\u70b9\u8d70 left - 1 \u6b65\uff0c\u6765\u5230 left \u8282\u70b9\u7684\u524d\u4e00\u4e2a\u8282\u70b9\r\n        # \u5efa\u8bae\u5199\u5728 for \u5faa\u73af\u91cc\uff0c\u8bed\u4e49\u6e05\u6670\r\n        for _ in range(left - 1):\r\n            pre = pre.next\r\n\r\n        # \u7b2c 2 \u6b65\uff1a\u4ece pre \u518d\u8d70 right - left + 1 \u6b65\uff0c\u6765\u5230 right \u8282\u70b9\r\n        right_node = pre\r\n        for _ in range(right - left + 1):\r\n            right_node = right_node.next\r\n        # \u7b2c 3 \u6b65\uff1a\u5207\u65ad\u51fa\u4e00\u4e2a\u5b50\u94fe\u8868\uff08\u622a\u53d6\u94fe\u8868\uff09\r\n        left_node = pre.next\r\n        curr = right_node.next\r\n\r\n        # \u6ce8\u610f\uff1a\u5207\u65ad\u94fe\u63a5\r\n        pre.next = None\r\n        right_node.next = None\r\n\r\n        # \u7b2c 4 \u6b65\uff1a\u540c\u7b2c 206 \u9898\uff0c\u53cd\u8f6c\u94fe\u8868\u7684\u5b50\u533a\u95f4\r\n        reverse_linked_list(left_node)\r\n        # \u7b2c 5 \u6b65\uff1a\u63a5\u56de\u5230\u539f\u6765\u7684\u94fe\u8868\u4e2d\r\n        pre.next = right_node\r\n        left_node.next = curr\r\n        return  dummy_node.next\r\n        # @lc code=end<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" width=\"664\" height=\"262\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-300.png\" alt=\"\" class=\"wp-image-2435\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-300.png 664w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-300-300x118.png 300w\" sizes=\"(max-width: 664px) 100vw, 664px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u4f60\u5355\u94fe\u8868\u7684\u5934\u6307\u9488&nbsp;head&nbsp;\u548c\u4e24\u4e2a\u6574\u6570&nbsp;left&nbsp;\u548c&nbsp;ri &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/01\/27\/leetcodeday92\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">leetcodeday92 &#8211;\u53cd\u8f6c\u94fe\u8868<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2434"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=2434"}],"version-history":[{"count":1,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2434\/revisions"}],"predecessor-version":[{"id":2436,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2434\/revisions\/2436"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=2434"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=2434"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=2434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}