{"id":2013,"date":"2022-05-18T12:15:36","date_gmt":"2022-05-18T12:15:36","guid":{"rendered":"https:\/\/www.zttofficial.com\/?p=2013"},"modified":"2022-05-18T12:15:38","modified_gmt":"2022-05-18T12:15:38","slug":"%e5%81%9a%e9%a1%8c%e7%ad%86%e8%a8%98%ef%bc%9amerge-two-sorted-lists-java","status":"publish","type":"post","link":"https:\/\/www.zttofficial.com\/?p=2013","title":{"rendered":"\u505a\u984c\u7b46\u8a18\uff1aMerge Two Sorted Lists (Java)"},"content":{"rendered":"\n<p><strong>\u984c\u76ee\u63cf\u8ff0\uff1a<\/strong><\/p>\n\n\n\n<p>\u7d66\u5b9a\u5169\u500b\u6392\u597d\u5e8f\u7684 linked list <kbd>list1<\/kbd> \u548c <kbd>list2<\/kbd> \u7684 head\u3002<\/p>\n\n\n\n<p>\u5c07\u524d\u5169\u500b linked list \u7684\u7bc0\u9ede\u62fc\u63a5\u5728\u4e00\u8d77\u5408\u4f75\u5230\u4e00\u500b\u5b8c\u6210\u6392\u5e8f\u7684 linked list \u4e2d\u3002<\/p>\n\n\n\n<p>\u8fd4\u56de\u5408\u4f75\u4e4b\u5f8c\u7684\u93c8\u7d50\u4e32\u5217\u7684 head\u3002<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">Input: list1 = [1,2,4], list2 = [1,3,4]\r\nOutput: [1,1,2,3,4,4]<\/code><\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">Input: list1 = [], list2 = []\r\nOutput: []<\/code><\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">Input: list1 = [], list2 = [0]\r\nOutput: [0]<\/code><\/pre>\n\n\n\n<p><strong>\u89e3\u6cd5\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">class Solution {\r\n    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {\r\n        ListNode result = new ListNode();\r\n        ListNode head = result;\r\n        while (true){\r\n            if (list1==null){\r\n                result.next=list2;\r\n                break;\r\n            }\r\n            if (list2==null){\r\n                result.next=list1;\r\n                break;\r\n            }\r\n            if (list1.val&lt;=list2.val){\r\n                result.next=list1;\r\n                list1=list1.next;\r\n            }\r\n            else {\r\n                result.next=list2;\r\n                list2=list2.next;\r\n            }\r\n            result=result.next;\r\n        }\r\n        return head.next;\r\n    }\r\n}<\/code><\/pre>\n\n\n\n<p>1 ms<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u984c\u76ee\u63cf\u8ff0\uff1a \u7d66\u5b9a\u5169\u500b\u6392\u597d\u5e8f\u7684 linked list list1 \u548c list2 \u7684 head\u3002 \u5c07\u524d\u5169\u500b linked list \u7684\u7bc0\u9ede\u62fc\u63a5\u5728\u4e00\u8d77\u5408\u4f75\u5230\u4e00\u500b\u5b8c\u6210\u6392\u5e8f\u7684 linked list \u4e2d\u3002 \u8fd4\u56de\u5408\u4f75\u4e4b\u5f8c\u7684\u93c8\u7d50\u4e32\u5217\u7684 head\u3002 Example 1: Example 2: Example 3: \u89e3\u6cd5\uff1a 1 ms<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,135],"tags":[137,141],"class_list":["post-2013","post","type-post","status-publish","format-standard","hentry","category-all","category-135","tag-leetcode","tag-linked-list"],"_links":{"self":[{"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=\/wp\/v2\/posts\/2013","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2013"}],"version-history":[{"count":1,"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=\/wp\/v2\/posts\/2013\/revisions"}],"predecessor-version":[{"id":2014,"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=\/wp\/v2\/posts\/2013\/revisions\/2014"}],"wp:attachment":[{"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}