{"id":995,"date":"2023-04-24T19:15:12","date_gmt":"2023-04-24T11:15:12","guid":{"rendered":"https:\/\/white-night.club\/?p=995"},"modified":"2023-11-09T11:07:56","modified_gmt":"2023-11-09T03:07:56","slug":"ad2","status":"publish","type":"post","link":"https:\/\/white-night.club\/index.php\/2023\/04\/24\/ad2\/","title":{"rendered":"24.\u00a0\u4e24\u4e24\u4ea4\u6362\u94fe\u8868\u4e2d\u7684\u8282\u70b9"},"content":{"rendered":"\n<p>\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\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter has-custom-border\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/10\/03\/swap_ex1.jpg\" alt=\"\" style=\"border-style:none;border-width:0px;border-radius:0px\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>head = [1,2,3,4]\n<strong>\u8f93\u51fa\uff1a<\/strong>[2,1,4,3]\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 = []\n<strong>\u8f93\u51fa\uff1a<\/strong>[]\n<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>head = [1]\n<strong>\u8f93\u51fa\uff1a<\/strong>[1]\n<\/pre>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul>\n<li>\u94fe\u8868\u4e2d\u8282\u70b9\u7684\u6570\u76ee\u5728\u8303\u56f4&nbsp;<code>[0, 100]<\/code>&nbsp;\u5185<\/li>\n\n\n\n<li><code>0 &lt;= Node.val &lt;= 100<\/code><\/li>\n<\/ul>\n\n\n\n<p class=\"has-text-align-center\"><strong>\u521d\u59cb\u4ee3\u7801<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><mark style=\"background-color:rgba(0, 0, 0, 0);color:#00ff2f\" class=\"has-inline-color\">\/**\n * Definition for singly-linked list.\n * struct ListNode {\n *     int val;\n *     ListNode *next;\n *     ListNode() : val(0), next(nullptr) {}\n *     ListNode(int x) : val(x), next(nullptr) {}\n *     ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n *\/<\/mark>\nclass Solution {\npublic:\n    ListNode* swapPairs(ListNode* head) {\n        \n    }\n};<\/code><\/pre>\n\n\n\n<div style=\"height:60px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<blockquote class=\"wp-block-quote has-luminous-vivid-amber-color has-text-color\">\n<p>\u89e3\u51b3\u601d\u8def<\/p>\n<cite><em>\u9012\u5f52\u5b9e\u73b0<\/em><\/cite><\/blockquote>\n\n\n\n<p>\u5148\u89c2\u5bdf\u9898\u76ee\u3002\u53d1\u73b0\u201c\u4e24\u4e24\u4ea4\u6362\u201d\u8fd9\u4e2a\u64cd\u4f5c\u5bf9\u4e8e\u4e00\u4e2a\u6709n\u4e2a\u5143\u7d20\uff08n&gt;1\uff09\u7684\u94fe\u8868\u6765\u8bf4\uff0c\u662f\u91cd\u590d\u64cd\u4f5c&#8211;\u5373\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u5904\u7406\u7684\u3002<\/p>\n\n\n\n<p>\u7c7b\u6bd4\u4e8e\u6590\u6ce2\u90a3\u5951\u6570\u5217\uff0c\u6211\u4eec\u5148\u770b\u770bn\u22641\u7684\u60c5\u51b5\uff0c\u53d1\u73b0\u51fd\u6570\u8fd4\u56de\u503c\u5c31\u662fhead\u3002\u90a3\u4e48\u53ef\u4ee5\u5199\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> ListNode* swapPairs(ListNode* head) {\n        if(head==NULL||head-&gt;next==NULL) <mark style=\"background-color:rgba(0, 0, 0, 0);color:#00ff2f\" class=\"has-inline-color\">\/\/n=0\u6216n=1<\/mark>\n            return head;\n    }<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\u5728\u770b\u770bn\u22652\u7684\u60c5\u51b5\u3002\u94fe\u8868\u4e2d\u7684\u7b2c\u4e8c\u4e2a\u8282\u70b9\u4e0e\u7b2c\u4e00\u4e2a\u8282\u70b9\u4ea4\u6362\u540e\uff0c\u7b2c\u4e8c\u4e2a\u8282\u70b9\u53d8\u6210\u4e86\u65b0\u7684\u5934\u8282\u70b9\uff1b\u539f\u672c\u7684\u5934\u8282\u70b9\u53d8\u6210\u4e86\u7b2c\u4e8c\u4e2a\u8282\u70b9\uff0c\u5e76\u4e14\u8fde\u63a5\u7740\u94fe\u8868\u540e\u7eed\u7684\u5185\u5bb9\u3002<\/p>\n\n\n\n<p>\u6839\u636e\u9012\u5f52\u7684\u76f8\u5173\u77e5\u8bc6\uff08\u628a\u9012\u5f52\u8fc7\u7a0b\u770b\u4f5c\u4e00\u4e2a\u6808\uff09\uff0c\u5047\u8bbe\u73b0\u5728\u94fe\u8868\u4e3a[1,2,3,4]\u3002\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\u4ee3\u7801:<\/p>\n\n\n\n<ul>\n<li>ListNode* tmp =head-&gt;next; \n<ul>\n<li>\u94fe\u8868\u4e2d\u7684\u6bcf\u4e24\u4e2a\u8282\u70b9\uff0c\u53d6\u7b2c\u4e00\u4e2a\u8282\u70b9\u4e3ahead\uff0c\u7b2c\u4e8c\u4e2a\u8282\u70b9\u4e3atmp\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u94fe\u8868\u4e2d\u5143\u7d20\u4e2a\u6570\u4e3a\u5947\u6570\uff0ctmp=NULL\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>head-&gt;next=swapPairs(tmp-&gt;next);\n<ul>\n<li>\u6bcf\u6b21\u9012\u5f52\u7684\u8fc7\u7a0b\u90fd\u88ab\u5b58\u5165\u4e86\u6808\u4e2d\uff0c\u76f4\u5230<mark style=\"background-color:rgba(0, 0, 0, 0);color:#fcb900\" class=\"has-inline-color\">\u4f20\u5165\u7684\u8282\u70b9\u4e3a\u7a7a<\/mark>\u6216<mark style=\"background-color:rgba(0, 0, 0, 0);color:#fcb900\" class=\"has-inline-color\">\u4f20\u5165\u8282\u70b9\u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\u4e3a\u7a7a<\/mark>\u3002<\/li>\n\n\n\n<li>\u56e0\u4e3a\u662f\u6bcf\u4e24\u4e2a\u8282\u70b9\u53d6\u4e00\u6b21head\u548ctmp\uff0c\u6240\u4ee5\u5f53tmp=NULL\uff08\u94fe\u8868\u4e2d\u5143\u7d20\u4e2a\u6570\u4e3a\u5947\u6570\uff09\uff0c\u6216tmp-&gt;next=NULL\uff08tmp\u662f\u94fe\u8868\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff09\u65f6\uff0c\u505c\u6b62\u9012\u5f52\u7684\u201c\u9012\u201d\u64cd\u4f5c\u3002<\/li>\n\n\n\n<li>\u201c\u5f52\u201d\u64cd\u4f5c\u5c31\u662f\u628a\u6808\u9876\u5143\u7d20\u5f39\u51fa\uff0c\u628a\u5b83\u7684\u503c\u4f20\u7ed9\u65b0\u7684\u6808\u9876\u5143\u7d20\u3002<\/li>\n\n\n\n<li>\u8be5\u64cd\u4f5c\u5c31\u662f\u5c06head\u7684next\u6307\u9488\u6307\u5411tmp\u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>tmp-&gt;next=head;\n<ul>\n<li>\u8be5\u64cd\u4f5c\u5c06tmp\u7684next\u6307\u9488\u6307\u5411head\u8282\u70b9\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>return tmp;\n<ul>\n<li>\u6700\u540e\u7684tmp\u4fbf\u662f\u65b0\u7684head\u8282\u70b9\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>\u628atmp-&gt;next=&#8230;\u7406\u89e3\u4e3a\u201c<mark style=\"background-color:rgba(0, 0, 0, 0);color:#fcb900\" class=\"has-inline-color\">\u5c06tmp\u8282\u70b9\u7684next\u6307\u9488\u6307\u5411\u67d0\u4e2a\u8282\u70b9<\/mark>\u201d\uff0c\u800c\u4e0d\u662f\u201ctmp\u8282\u70b9\u7684next\u6307\u9488\u7b49\u4e8e\u67d0\u4e2a\u8282\u70b9\u201d\u5c31\u5f88\u597d\u660e\u767d\u4e86\uff0c\u4ee5\u4e0b\u662f\u6d41\u7a0b\u56fe\u548c\u5b8c\u6574\u4ee3\u7801<\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong>\u6d41\u7a0b\u56fe<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large has-custom-border\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"775\" src=\"https:\/\/white-night.club\/wp-content\/uploads\/2023\/04\/nodeswap-1024x775.png\" alt=\"\" class=\"wp-image-1007\" style=\"border-style:none;border-width:0px;border-radius:0px\" srcset=\"https:\/\/white-night.club\/wp-content\/uploads\/2023\/04\/nodeswap-1024x775.png 1024w, https:\/\/white-night.club\/wp-content\/uploads\/2023\/04\/nodeswap-300x227.png 300w, https:\/\/white-night.club\/wp-content\/uploads\/2023\/04\/nodeswap-768x581.png 768w, https:\/\/white-night.club\/wp-content\/uploads\/2023\/04\/nodeswap-1536x1163.png 1536w, https:\/\/white-night.club\/wp-content\/uploads\/2023\/04\/nodeswap.png 1655w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-align-center\"><strong>\u5b8c\u6574\u4ee3\u7801<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><mark style=\"background-color:rgba(0, 0, 0, 0);color:#00ff2f\" class=\"has-inline-color\">\/**\n * Definition for singly-linked list.\n * struct ListNode {\n *     int val;\n *     ListNode *next;\n *     ListNode() : val(0), next(nullptr) {}\n *     ListNode(int x) : val(x), next(nullptr) {}\n *     ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n *\/<\/mark>\nclass Solution {\npublic:\n    ListNode* swapPairs(ListNode* head) {\n        if(head==NULL||head->next==NULL)\n            return head;\n        ListNode* tmp=head->next;\n        head->next=swapPairs(tmp->next);\n        tmp->next=head;\n        return tmp;\n    }\n};<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>LeetCode\u9898\u76ee\uff1aC++\u9012\u5f52<\/p>\n","protected":false},"author":1,"featured_media":1007,"comment_status":"open","ping_status":"closed","sticky":false,"template":"base","format":"standard","meta":{"footnotes":""},"categories":[42,25],"tags":[38],"_links":{"self":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/995"}],"collection":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/comments?post=995"}],"version-history":[{"count":0,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/995\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media\/1007"}],"wp:attachment":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media?parent=995"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/categories?post=995"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/tags?post=995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}