{"id":714,"date":"2020-06-24T01:24:42","date_gmt":"2020-06-23T17:24:42","guid":{"rendered":"http:\/\/www.guanhaobo.cn\/?p=714"},"modified":"2020-06-24T01:24:42","modified_gmt":"2020-06-23T17:24:42","slug":"jz16-%e5%90%88%e5%b9%b6%e4%b8%a4%e4%b8%aa%e6%8e%92%e5%ba%8f%e7%9a%84%e9%93%be%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=714","title":{"rendered":"JZ16 \u2014 \u5408\u5e76\u4e24\u4e2a\u6392\u5e8f\u7684\u94fe\u8868"},"content":{"rendered":"<h3>0 \u9898\u76ee\u63cf\u8ff0<\/h3>\n<p>\u8f93\u5165\u4e24\u4e2a\u5355\u8c03\u9012\u589e\u7684\u94fe\u8868\uff0c\u8f93\u51fa\u4e24\u4e2a\u94fe\u8868\u5408\u6210\u540e\u7684\u94fe\u8868\uff0c\u5f53\u7136\u6211\u4eec\u9700\u8981\u5408\u6210\u540e\u7684\u94fe\u8868\u6ee1\u8db3\u5355\u8c03\u4e0d\u51cf\u89c4\u5219\u3002<\/p>\n<h3>1 \u89e3\u6cd5\u4e00<\/h3>\n<p>\u4f7f\u7528\u4e24\u4e2a\u6307\u9488\uff0c\u5206\u522b\u6307\u5411\u4e24\u4e2a\u94fe\u8868\uff0c\u6bcf\u6b21\u6dfb\u52a0\u8f83\u5c0f\u7684\u90a3\u4e2a\u5230\u65b0\u7684\u94fe\u8868\u4e2d\u5373\u53ef\u3002\u6ce8\u610f\uff0c\u5f53\u5176\u4e2d\u4e00\u4e2a\u94fe\u8868\u7684\u7ed3\u70b9\u5168\u90e8\u90fd\u88ab\u6dfb\u52a0\u540e\uff0c\u5c06\u53e6\u5916\u4e00\u4e2a\u94fe\u8868\u5269\u4f59\u90e8\u5206\u5168\u90e8\u6dfb\u52a0\u5230\u65b0\u94fe\u8868\u3002<br \/>\n\u521b\u5efa\u4e00\u4e2a\u65e0\u7528\u7684\u5934\u7ed3\u70b9\uff0c\u4fbf\u4e8e\u64cd\u4f5c\u3002<\/p>\n<h4>1.1 \u5f00\u8f9f\u65b0\u7684\u7a7a\u95f4\uff0c\u4e0d\u5f71\u54cd\u539f\u94fe\u8868<\/h4>\n<h5>1.1.1 C++<\/h5>\n<pre><code class=\"language-cpp line-numbers\">\/*\nstruct ListNode {\n    int val;\n    struct ListNode *next;\n    ListNode(int x) :\n            val(x), next(NULL) {\n    }\n};*\/\nclass Solution\n{\npublic:\n    ListNode *Merge(ListNode *pHead1, ListNode *pHead2)\n    {\n        ListNode *head = new ListNode(0), *current = head;\n        while (pHead1 &amp;&amp; pHead2)\n        {\n            if (pHead1-&gt;val &lt;= pHead2-&gt;val)\n            {\n                current-&gt;next = new ListNode(pHead1-&gt;val);\n                pHead1 = pHead1-&gt;next;\n            }\n            else\n            {\n                current-&gt;next = new ListNode(pHead2-&gt;val);\n                pHead2 = pHead2-&gt;next;\n            }\n            current = current-&gt;next;\n        }\n        while (pHead1)\n        {\n            current-&gt;next = new ListNode(pHead1-&gt;val);\n            pHead1 = pHead1-&gt;next;\n            current = current-&gt;next;\n        }\n        while (pHead2)\n        {\n            current-&gt;next = new ListNode(pHead2-&gt;val);\n            pHead2 = pHead2-&gt;next;\n            current = current-&gt;next;\n        }\n        return head-&gt;next;\n    }\n};\n<\/code><\/pre>\n<h5>1.1.2 Java<\/h5>\n<pre><code class=\"language-java line-numbers\">\/*\npublic class ListNode {\n    int val;\n    ListNode next = null;\n\n    ListNode(int val) {\n        this.val = val;\n    }\n}*\/\npublic class Solution {\n    public ListNode Merge(ListNode list1, ListNode list2) {\n        ListNode head = new ListNode(0), current = head;\n        while (list1 != null &amp;&amp; list2 != null) {\n            if (list1.val &lt;= list2.val) {\n                current.next = new ListNode(list1.val);\n                list1 = list1.next;\n            } else {\n                current.next = new ListNode(list2.val);\n                list2 = list2.next;\n            }\n            current = current.next;\n        }\n        while (list1 != null) {\n            current.next = new ListNode(list1.val);\n            list1 = list1.next;\n            current = current.next;\n        }\n        while (list2 != null) {\n            current.next = new ListNode(list2.val);\n            list2 = list2.next;\n            current = current.next;\n        }\n        return head.next;\n    }\n}\n<\/code><\/pre>\n<h4>1.2 \u4e0d\u5f00\u8f9f\u65b0\u7a7a\u95f4\uff0c\u76f4\u63a5\u5728\u539f\u94fe\u8868\u8fdb\u884c\u4fee\u6539<\/h4>\n<h5>1.2.1 C++<\/h5>\n<pre><code class=\"language-cpp line-numbers\">\/*\nstruct ListNode {\n    int val;\n    struct ListNode *next;\n    ListNode(int x) :\n            val(x), next(NULL) {\n    }\n};*\/\nclass Solution\n{\npublic:\n    ListNode *Merge(ListNode *pHead1, ListNode *pHead2)\n    {\n        ListNode *head = new ListNode(0), *current = head;\n        while (pHead1 &amp;&amp; pHead2)\n        {\n            if (pHead1-&gt;val &lt;= pHead2-&gt;val)\n            {\n                current-&gt;next = pHead1;\n                pHead1 = pHead1-&gt;next;\n            }\n            else\n            {\n                current-&gt;next = pHead2;\n                pHead2 = pHead2-&gt;next;\n            }\n            current = current-&gt;next;\n        }\n        current-&gt;next = pHead1 ? pHead1 : pHead2;\n        return head-&gt;next;\n    }\n};\n<\/code><\/pre>\n<h5>1.2.2 Java<\/h5>\n<pre><code class=\"language-java line-numbers\">\/*\npublic class ListNode {\n    int val;\n    ListNode next = null;\n\n    ListNode(int val) {\n        this.val = val;\n    }\n}*\/\npublic class Solution {\n    public ListNode Merge(ListNode list1, ListNode list2) {\n        ListNode head = new ListNode(0), current = head;\n        while (list1 != null &amp;&amp; list2 != null) {\n            if (list1.val &lt;= list2.val) {\n                current.next = list1;\n                list1 = list1.next;\n            } else {\n                current.next = list2;\n                list2 = list2.next;\n            }\n            current = current.next;\n        }\n        current.next = list1 != null ? list1 : list2;\n        return head.next;\n    }\n}\n<\/code><\/pre>\n<h3>2 \u89e3\u6cd5\u4e8c<\/h3>\n<p>\u4f7f\u7528\u9012\u5f52\u5b8c\u6210\u4e0a\u8ff0\u8fc7\u7a0b\uff0c\u53ef\u4ee5\u5f00\u8f9f\u65b0\u7a7a\u95f4\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u5728\u539f\u94fe\u8868\u4e0a\u4fee\u6539\u3002\u8fd9\u91cc\u662f\u76f4\u63a5\u5728\u539f\u94fe\u8868\u4e0a\u4fee\u6539\u3002<\/p>\n<h5>2.1 C++<\/h5>\n<pre><code class=\"language-cpp line-numbers\">\/*\nstruct ListNode {\n    int val;\n    struct ListNode *next;\n    ListNode(int x) :\n            val(x), next(NULL) {\n    }\n};*\/\nclass Solution\n{\npublic:\n    ListNode *Merge(ListNode *pHead1, ListNode *pHead2)\n    {\n        ListNode *head;\n        if (pHead1 &amp;&amp; pHead2)\n        {\n            if (pHead1-&gt;val &lt;= pHead2-&gt;val)\n            {\n                head = pHead1;\n                pHead1-&gt;next = Merge(pHead1-&gt;next, pHead2);\n            }\n            else\n            {\n                head = pHead2;\n                pHead2-&gt;next = Merge(pHead2-&gt;next, pHead1);\n            }\n        }\n        else\n            head = pHead1 ? pHead1 : pHead2;\n        return head;\n    }\n};\n<\/code><\/pre>\n<h5>2.2 Java<\/h5>\n<pre><code class=\"language-java line-numbers\">\/*\npublic class ListNode {\n    int val;\n    ListNode next = null;\n\n    ListNode(int val) {\n        this.val = val;\n    }\n}*\/\npublic class Solution {\n    public ListNode Merge(ListNode list1, ListNode list2) {\n        ListNode head;\n        if (list1 != null &amp;&amp; list2 != null) {\n            if (list1.val &lt;= list2.val) {\n                head = list1;\n                list1.next = Merge(list1.next, list2);\n            } else {\n                head = list2;\n                list2.next = Merge(list2.next, list1);\n            }\n        } else\n            head = list1 != null ? list1 : list2;\n        return head;\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>0 \u9898\u76ee\u63cf\u8ff0 \u8f93\u5165\u4e24\u4e2a\u5355\u8c03\u9012\u589e\u7684\u94fe\u8868\uff0c\u8f93\u51fa\u4e24\u4e2a\u94fe\u8868\u5408\u6210\u540e\u7684\u94fe\u8868\uff0c\u5f53\u7136\u6211\u4eec\u9700\u8981\u5408\u6210\u540e\u7684\u94fe\u8868\u6ee1\u8db3\u5355\u8c03\u4e0d\u51cf\u89c4\u5219\u3002 1 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[39],"class_list":["post-714","post","type-post","status-publish","format-standard","hentry","category-algo","tag-offer"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/714","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=714"}],"version-history":[{"count":0,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/714\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=714"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=714"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=714"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}