{"id":194,"date":"2023-08-06T14:35:55","date_gmt":"2023-08-06T06:35:55","guid":{"rendered":"http:\/\/lingbo.online\/?p=194"},"modified":"2023-08-21T14:02:23","modified_gmt":"2023-08-21T06:02:23","slug":"leecode_link","status":"publish","type":"post","link":"https:\/\/lingbo.online\/index.php\/algorithm_learning\/leecode_link\/","title":{"rendered":"LeeCode\u7b97\u6cd5\u603b\u7ed3\u2014\u2014\u94fe\u8868"},"content":{"rendered":"<h3>\u9012\u5f52\u7c7b<\/h3>\n<h4>LeeCode 24.\u4e24\u4e24\u4ea4\u6362\u94fe\u8868\u4e2d\u7684\u8282\u70b9<\/h4>\n<h5>\u9898\u76ee\u63cf\u8ff0<\/h5>\n<p>\u9898\u76ee\u94fe\u63a5\uff1a<a href=\"https:\/\/leetcode.cn\/problems\/swap-nodes-in-pairs\/description\/\" target=\"_blank\"  rel=\"nofollow\" >https:\/\/leetcode.cn\/problems\/swap-nodes-in-pairs\/description\/<\/a><br \/>\n\u7ed9\u4f60\u4e00\u4e2a\u94fe\u8868\uff0c\u4e24\u4e24\u4ea4\u6362\u5176\u4e2d\u76f8\u90bb\u7684\u8282\u70b9\uff0c\u5e76\u8fd4\u56de\u4ea4\u6362\u540e\u94fe\u8868\u7684\u5934\u8282\u70b9\u3002\u4f60\u5fc5\u987b\u5728\u4e0d\u4fee\u6539\u8282\u70b9\u5185\u90e8\u7684\u503c\u7684\u60c5\u51b5\u4e0b\u5b8c\u6210\u672c\u9898\uff08\u5373\uff0c\u53ea\u80fd\u8fdb\u884c\u8282\u70b9\u4ea4\u6362\uff09:<\/p>\n<p><strong>\u793a\u4f8b1\uff1a<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/lingbo.online\/wp-content\/uploads\/2023\/08\/swap_ex1.jpg\" alt=\"\" \/><\/p>\n<pre><code>\u8f93\u5165\uff1ahead = [1,2,3,4]\n\u8f93\u51fa\uff1a[2,1,4,3]<\/code><\/pre>\n<h5>\u89e3\u9898\u601d\u8def<\/h5>\n<h6>\u8fed\u4ee3\u89e3\u6cd5<\/h6>\n<ol>\n<li>\u521b\u5efa\u54e8\u5175\u8282\u70b9dummy\uff0c\u8868\u793a\u8282\u70b90<\/li>\n<li>\u5c06node0\u6307\u5411node2<\/li>\n<li>\u5c06node2.\u6307\u5411node1<\/li>\n<li>\u5c06node1\u6307\u5411node3<\/li>\n<li>\u66f4\u65b0node0\u4e3anode1\uff0c\u66f4\u65b0node1\u4e3anode3<\/li>\n<li>\u5982\u679cnode1\u548cnode1.next\u90fd\u4e0d\u4e3a\u7a7a\uff0c\u5c31\u56de\u5230\u7b2c2\u6b65\uff0c\u8fdb\u884c\u4e0b\u4e00\u8f6e\u4ea4\u6362<\/li>\n<li>\u8fd4\u56dedummy.next\uff0c\u4f5c\u4e3a\u65b0\u94fe\u8868\u7684\u5934\u8282\u70b9<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/lingbo.online\/wp-content\/uploads\/2023\/08\/\u5fae\u4fe1\u622a\u56fe_20230806142025.png\" alt=\"\" \/><\/p>\n<p><strong>\u590d\u6742\u5ea6\u5206\u6790<\/strong><\/p>\n<ul>\n<li>\u65f6\u95f4\u590d\u6742\u5ea6\uff1aO(n)<\/li>\n<li>\u7a7a\u95f4\u590d\u6742\u5ea6\uff1aO(1)<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/p>\n<pre><code class=\"language-python\">class Solution:\n    def swapPairs(self, head: Optional[ListNode]) -&gt; Optional[ListNode]:\n        node0 = dummy = ListNode(next=head)  # \u7528\u54e8\u5175\u8282\u70b9\u7b80\u5316\u4ee3\u7801\u903b\u8f91\n        node1 = head\n        while node1 and node1.next:  # \u81f3\u5c11\u6709\u4e24\u4e2a\u8282\u70b9\n            node2 = node1.next\n            node3 = node2.next\n\n            node0.next = node2  # 0 -&gt; 2\n            node2.next = node1  # 2 -&gt; 1\n            node1.next = node3  # 1 -&gt; 3\n\n            node0 = node1  # \u4e0b\u4e00\u8f6e\u4ea4\u6362\uff0c0 \u662f 1\n            node1 = node3  # \u4e0b\u4e00\u8f6e\u4ea4\u6362\uff0c1 \u662f 3\n        return dummy.next  # \u8fd4\u56de\u65b0\u94fe\u8868\u7684\u5934\u8282\u70b9<\/code><\/pre>\n<h6>\u9012\u5f52\u89e3\u6cd5<\/h6>\n<p>\u5c06\u65b9\u6cd5\u4e00\u6539\u9020\u4e3a\u9012\u5f52\u89e3\u6cd5\uff0c\u5219\uff1a<\/p>\n<ul>\n<li>\u9012\u5f52\u8fb9\u754c\uff08\u7ec8\u6b62\u6761\u4ef6\uff09\uff1a\u94fe\u8868\u4e2d\u6ca1\u6709\u8282\u70b9\u6216\u94fe\u8868\u53ea\u6709\u4e00\u4e2a\u8282\u70b9\uff0c\u6b64\u65f6\u65e0\u6cd5\u518d\u8fdb\u884c\u4ea4\u6362<\/li>\n<li>\u7528head\u8868\u793a\u539f\u59cb\u94fe\u8868\u5934\u8282\u70b9\uff0c\u65b0\u94fe\u8868\u7b2c\u4e8c\u4e2a\u8282\u70b9\uff0c\u7528newhead\u8868\u793a\u65b0\u94fe\u8868\u5934\u8282\u70b9\uff0c\u539f\u59cb\u94fe\u8868\u7b2c\u4e8c\u4e2a\u8282\u70b9<\/li>\n<li>\u5c06\u5176\u4f59\u8282\u70b9\u4e24\u4e24\u4ea4\u6362\uff0c\u5373\uff1ahead.next=swapPairs(newhead.next)<\/li>\n<li>\u4ea4\u6362\u540e\u7684\u5934\u8282\u70b9\u4e3ahead\u7684\u4e0b\u4e00\u4e2a\u8282\u70b9<\/li>\n<li>\u8fd4\u56de\u65b0\u94fe\u8868\u7684\u5934\u8282\u70b9newhead<\/li>\n<\/ul>\n<p><strong>\u590d\u6742\u5ea6\u5206\u6790<\/strong><\/p>\n<ul>\n<li>\u65f6\u95f4\u590d\u6742\u5ea6\uff1aO(n)\uff0c\u5176\u4e2dn\u662f\u94fe\u8868\u7684\u8282\u70b9\u6570\u91cf\u3002\u9700\u8981\u5bf9\u6bcf\u4e2a\u8282\u70b9\u8fdb\u884c\u66f4\u65b0\u6307\u9488\u7684\u64cd\u4f5c<\/li>\n<li>\u7a7a\u95f4\u590d\u6742\u5ea6\uff1aO(n)\uff0c\u5176\u4e2dn\u662f\u94fe\u8868\u7684\u8282\u70b9\u6570\u91cf\u3002\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3b\u8981\u53d6\u51b3\u4e8e\u9012\u5f52\u8c03\u7528\u7684\u6808\u7a7a\u95f4<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/p>\n<pre><code class=\"language-python\">class Solution:\n    def swapPairs(self, head: ListNode) -&gt; ListNode:\n        if not head or not head.next:\n            return head\n        newHead = head.next\n        head.next = self.swapPairs(newHead.next)\n        newHead.next = head\n        return newHead<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9012\u5f52\u7c7b LeeCode 24.\u4e24\u4e24\u4ea4\u6362\u94fe\u8868\u4e2d\u7684\u8282\u70b9 \u9898\u76ee\u63cf\u8ff0 \u9898\u76ee\u94fe\u63a5\uff1ahttps:\/\/leetcode.cn\/problems\/ &#8230;<\/p>\n","protected":false},"author":1,"featured_media":230,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[23,2],"tags":[25,26],"class_list":["post-194","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-leecode","category-algorithm_learning","tag-leecode","tag-26"],"_links":{"self":[{"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/posts\/194","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/comments?post=194"}],"version-history":[{"count":2,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/posts\/194\/revisions"}],"predecessor-version":[{"id":198,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/posts\/194\/revisions\/198"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/media\/230"}],"wp:attachment":[{"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/media?parent=194"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/categories?post=194"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/tags?post=194"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}