{"id":1866,"date":"2016-03-07T18:38:31","date_gmt":"2016-03-07T10:38:31","guid":{"rendered":"http:\/\/boweihe.me\/?p=1866"},"modified":"2016-03-07T18:38:31","modified_gmt":"2016-03-07T10:38:31","slug":"leetcode-24-swap-nodes-in-pairs","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=1866","title":{"rendered":"LeetCode 24. Swap Nodes in Pairs"},"content":{"rendered":"<p><strong>\u9898\u76ee\uff1a<\/strong>https:\/\/leetcode.com\/problems\/swap-nodes-in-pairs\/<br \/>\n<strong>\u601d\u8def\uff1a<\/strong><br \/>\n\u771f\u662f\u4e2a\u8ba9\u4eba\u5934\u6655\u7684\u95ee\u9898\uff0c\u5934\u6655\u7684\u5730\u65b9\u5728\u4e8e\u6307\u9488\uff0c\u4e0d\u8fc7\u8bb0\u4f4f\u6307\u9488\u5176\u5b9e\u5c31\u662f\u4e2a\u5730\u5740\u5c31\u884c\u4e86\u3002\u9898\u76ee\u7684\u610f\u601d\u662f\u5947\u6570\u9879\u548c\u5076\u6570\u9879\u4ea4\u6362\uff0c\u5148\u5047\u8bbe\u8f93\u5165\u6570\u7ec4\u5c31\u662f\u5076\u6570\u4e2a\u6570\u5b57\u7684\uff0c\u90a3\u5176\u5b9e\u5c31\u662fa[i]\u4e0ea[i+1]\u4e3a\u4e00\u5bf9\u4e00\u5bf9\u7684\u4ea4\u6362\uff0c\u4f46\u662f\u7531\u4e8e\u662f\u5355\u94fe\u8868\u800c\u4e14\u4e0d\u5141\u8bb8\u505a\u503c\u4ea4\u6362\uff0c\u53ea\u80fd\u901a\u8fc7\u6539\u53d8next\u6307\u9488\u5b9e\u73b0\u4e86\u3002<br \/>\n\u6211\u4eec\u5047\u8bbe\u6709\u8fd9\u4e48\u4e2a\u6570\u7ec4\/\u5730\u5740\uff1a<\/p>\n<pre class=\"lang:default decode:true\">value   [1,   2,   3,   4,   5,   6]\naddress [0x1, 0x2, 0x3, 0x4, 0x5, 0x6]<\/pre>\n<p>\u5f88\u663e\u7136\u7684\uff0c\u5982\u679c\u4e24\u4e2a\u6570\u7684\u503c\u8fdb\u884c\u4ea4\u6362\uff0c\u6211\u4eec\u5c31\u9700\u8981\u627e\u4e2a\u4e34\u65f6\u53d8\u91cf\u5b58\u7740\uff0c\u8fd9\u91cc\u4e5f\u662f\u4e00\u6837\u3002\u672c\u9898\u7684\u4e3b\u8981\u601d\u8def\u5c31\u662f\uff0c\u4e0d\u80fd\u4e71\uff0c\u4e00\u5bf9\u4e00\u5bf9\u7684\u6765&#8230;<br \/>\n\u53ea\u770b\u4e2d\u95f4\u76843,4\u4e24\u9879\u4e3a\u4e00\u5bf9\uff0c\u4e14\u5047\u8bbe\u5176\u4ed6\u4e00\u5207\u90fd\u5b89\u6392\u59a5\u5f53\u4e86[2, 1, (\u5f53\u524d\u5bf9), 5, 6]\uff0c\u90a3\u4e48\u53d8\u6362\u540e\u7684\u7ed3\u679c\u662f [3]-&gt;next = [5], [4]-&gt;next = [3]\u5e76\u4e14[1]-&gt;next = [4]. \u7136\u540e\u5177\u4f53\u800c\u8a00\u5206\u4e0b\u9762\u51e0\u6b65\uff1a<\/p>\n<ol>\n<li>\u5f53\u524d\u5de5\u4f5c\u6307\u9488curr\u662f[3],\u8bb0\u4f4f[3]\u7684\u540e\u7eed\u8282\u70b9[4]\u7684\u4f4d\u7f6e\uff0c\u5373temp = 0x4;<\/li>\n<li>\u628a[3]\u548c[5]\u8fde\u4e0a\uff0c\u5373 [3]-&gt;next = temp-&gt;next = 0x5;<\/li>\n<li>\u628a[4]\u653e\u5230[3]\u524d\u9762\uff0c\u5373temp-&gt;next = [3] (0x3);<\/li>\n<li>\u628a[2]\u548c[4]\u8fde\u4e0a\uff0c\u5373[2]-&gt;next = temp; \u6240\u4ee5\u6574\u4e2a\u73af\u8282\u4e2d\u8fd8\u9700\u8981\u5b58\u4e00\u4e2a[3]\u7684\u524d\u5e8f\u8282\u70b9\u7684\u6307\u9488(\u8fd9\u91cc\u662f0x2);<\/li>\n<li>\u632a\u52a8\u6307\u9488\uff0c\u6ce8\u610f\u7684\u662f\u53ea\u8981\u5f80\u540enext\u4e00\u4e0b\u5c31\u884c\u4e86\uff0c\u56e0\u4e3a\u76ee\u524d\u7684\u6392\u5e8f\u5df2\u7ecf\u662f[2,1, 4,3, &#8230;]\u4e86\uff1acurr = curr-&gt;next (0x5);<\/li>\n<\/ol>\n<p>\u641e\u5b9a\u4e3b\u8981\u90e8\u5206\u540e\u8981\u6ce8\u610f\u7279\u6b8a\u60c5\u51b5\uff1a0\u8f93\u5165\uff0c1\u8f93\u5165\uff0c\u5947\u6570\u9879\u8f93\u5165\uff08\u6700\u540e\u4e00\u9879\u4e0d\u5904\u7406\u5c31\u884c\uff09<br \/>\n<strong>\u4ee3\u7801:<\/strong><\/p>\n<pre class=\"lang:c++ decode:true \">\/**\n * Definition for singly-linked list.\n * struct ListNode {\n *     int val;\n *     ListNode *next;\n *     ListNode(int x) : val(x), next(NULL) {}\n * };\n *\/\nclass Solution {\npublic:\n    ListNode* swapPairs(ListNode* head) {\n        if(head == NULL)\n            return head;\n        if(head-&gt;next == NULL)\n            return head;\n        ListNode* curr = head;\n        ListNode* prev = NULL;\n        head = head-&gt;next;\n        ListNode* temp;\n        while(curr != NULL &amp;&amp; curr-&gt;next != NULL){\n            temp = curr-&gt;next;\n            if(prev != NULL) {\n                prev-&gt;next = temp;\n            }\n            curr-&gt;next = temp-&gt;next;\n            temp-&gt;next = curr;\n            prev = curr;\n            \/\/Change of current node\n            curr = curr-&gt;next;\n        }\n        return head;\n    }\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\uff1ahttps:\/\/leetcode.com\/problems\/swap-nodes-in-pairs\/ \u601d\u8def\uff1a \u771f\u662f\u4e2a\u8ba9\u4eba\u5934\u6655\u7684\u95ee\u9898\uff0c\u5934\u6655\u7684\u5730\u65b9\u5728\u4e8e\u6307\u9488\uff0c\u4e0d\u8fc7\u8bb0\u4f4f\u6307\u9488\u5176\u5b9e\u5c31\u662f\u4e2a\u5730\u5740\u5c31\u884c\u4e86\u3002\u9898\u76ee\u7684\u610f\u601d\u662f\u5947\u6570\u9879\u548c\u5076\u6570\u9879\u4ea4\u6362\uff0c\u5148\u5047\u8bbe\u8f93\u5165\u6570\u7ec4\u5c31\u662f\u5076\u6570\u4e2a\u6570\u5b57\u7684\uff0c\u90a3\u5176\u5b9e\u5c31\u662fa[i]\u4e0ea[i+1]\u4e3a\u4e00\u5bf9\u4e00\u5bf9\u7684\u4ea4\u6362\uff0c\u4f46\u662f\u7531\u4e8e\u662f\u5355\u94fe\u8868\u800c\u4e14\u4e0d\u5141\u8bb8\u505a\u503c\u4ea4\u6362\uff0c\u53ea\u80fd\u901a\u8fc7\u6539\u53d8next\u6307\u9488\u5b9e\u73b0\u4e86\u3002 \u6211\u4eec\u5047\u8bbe\u6709\u8fd9\u4e48\u4e2a\u6570\u7ec4\/\u5730\u5740\uff1a value [1, 2, 3, 4, 5, 6] address [0x1, 0x2, 0x3, 0x4, 0x5, 0x6] \u5f88\u663e\u7136\u7684\uff0c\u5982\u679c\u4e24\u4e2a\u6570\u7684\u503c\u8fdb\u884c\u4ea4\u6362\uff0c\u6211\u4eec\u5c31\u9700\u8981\u627e\u4e2a\u4e34\u65f6\u53d8\u91cf\u5b58\u7740\uff0c\u8fd9\u91cc\u4e5f\u662f\u4e00\u6837\u3002\u672c\u9898\u7684\u4e3b\u8981\u601d\u8def\u5c31\u662f\uff0c\u4e0d\u80fd\u4e71\uff0c\u4e00\u5bf9\u4e00\u5bf9\u7684\u6765&#8230; \u53ea\u770b\u4e2d\u95f4\u76843,4\u4e24\u9879\u4e3a\u4e00\u5bf9\uff0c\u4e14\u5047\u8bbe\u5176\u4ed6\u4e00\u5207\u90fd\u5b89\u6392\u59a5\u5f53\u4e86[2, 1, (\u5f53\u524d\u5bf9), 5, 6]\uff0c\u90a3\u4e48\u53d8\u6362\u540e\u7684\u7ed3\u679c\u662f [3]-&gt;next = [5], [4]-&gt;next = [3]\u5e76\u4e14[1]-&gt;next = [4]. \u7136\u540e\u5177\u4f53\u800c\u8a00\u5206\u4e0b\u9762\u51e0\u6b65\uff1a \u5f53\u524d\u5de5\u4f5c\u6307\u9488curr\u662f[3],\u8bb0\u4f4f[3]\u7684\u540e\u7eed\u8282\u70b9[4]\u7684\u4f4d\u7f6e\uff0c\u5373temp = 0x4; \u628a[3]\u548c[5]\u8fde\u4e0a\uff0c\u5373 [3]-&gt;next = temp-&gt;next = 0x5; \u628a[4]\u653e\u5230[3]\u524d\u9762\uff0c\u5373temp-&gt;next = [3] (0x3); \u628a[2]\u548c[4]\u8fde\u4e0a\uff0c\u5373[2]-&gt;next = temp; \u6240\u4ee5\u6574\u4e2a\u73af\u8282\u4e2d\u8fd8\u9700\u8981\u5b58\u4e00\u4e2a[3]\u7684\u524d\u5e8f\u8282\u70b9\u7684\u6307\u9488(\u8fd9\u91cc\u662f0x2); \u632a\u52a8\u6307\u9488\uff0c\u6ce8\u610f\u7684\u662f\u53ea\u8981\u5f80\u540enext\u4e00\u4e0b\u5c31\u884c\u4e86\uff0c\u56e0\u4e3a\u76ee\u524d\u7684\u6392\u5e8f\u5df2\u7ecf\u662f[2,1, 4,3, &#8230;]\u4e86\uff1acurr = curr-&gt;next [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[66],"class_list":["post-1866","post","type-post","status-publish","format-standard","hentry","category-study","tag-leetcode-oj"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1866","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1866"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1866\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1866"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1866"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1866"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}