{"id":7722,"date":"2020-11-28T11:07:25","date_gmt":"2020-11-28T19:07:25","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7722"},"modified":"2020-11-28T11:08:10","modified_gmt":"2020-11-28T19:08:10","slug":"leetcode-1669-merge-in-between-linked-lists","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/list\/leetcode-1669-merge-in-between-linked-lists\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1669. Merge In Between Linked Lists"},"content":{"rendered":"\n<p>You are given two linked lists:&nbsp;<code>list1<\/code>&nbsp;and&nbsp;<code>list2<\/code>&nbsp;of sizes&nbsp;<code>n<\/code>&nbsp;and&nbsp;<code>m<\/code>&nbsp;respectively.<\/p>\n\n\n\n<p>Remove&nbsp;<code>list1<\/code>&#8216;s nodes from the&nbsp;<code>a<sup>th<\/sup><\/code>&nbsp;node to the&nbsp;<code>b<sup>th<\/sup><\/code>&nbsp;node, and put&nbsp;<code>list2<\/code>&nbsp;in their place.<\/p>\n\n\n\n<p>The blue edges and nodes in the following figure incidate the result:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/11\/05\/fig1.png\" alt=\"\"\/><\/figure>\n\n\n\n<p><em>Build the result list and return its head.<\/em><\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/11\/05\/merge_linked_list_ex1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> list1 = [0,1,2,3,4,5], a = 3, b = 4, list2 = [1000000,1000001,1000002]\n<strong>Output:<\/strong> [0,1,2,1000000,1000001,1000002,5]\n<strong>Explanation:<\/strong> We remove the nodes 3 and 4 and put the entire list2 in their place. The blue edges and nodes in the above figure indicate the result.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/11\/05\/merge_linked_list_ex2.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> list1 = [0,1,2,3,4,5,6], a = 2, b = 5, list2 = [1000000,1000001,1000002,1000003,1000004]\n<strong>Output:<\/strong> [0,1,1000000,1000001,1000002,1000003,1000004,6]\n<strong>Explanation:<\/strong> The blue edges and nodes in the above figure indicate the result.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>3 &lt;= list1.length &lt;= 10<sup>4<\/sup><\/code><\/li><li><code>1 &lt;= a &lt;= b &lt; list1.length - 1<\/code><\/li><li><code>1 &lt;= list2.length &lt;= 10<sup>4<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: List Operations<\/strong><\/h2>\n\n\n\n<p>Find the following nodes:<br>1. previous node to the a-th node: prev_a<br>2. the b-th node: node_b<br>3. tail node of list2: tail2<br><br>prev_a-&gt;next = list2<br>tail2-&gt;next = node_b<br><br>return list1<\/p>\n\n\n\n<p>Time complexity: O(m+n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  ListNode* mergeInBetween(ListNode* list1, int a, int b, ListNode* list2) {\n    ListNode dummy(0, list1);\n    ListNode* prev_a = &dummy;\n    for (int i = 0; i < a; ++i) prev_a = prev_a->next;\n    ListNode* node_b = prev_a->next;\n    for (int i = a; i <= b; ++i) node_b = node_b->next;\n    ListNode* tail2 = list2;\n    while (tail2->next) tail2 = tail2->next;\n    \n    prev_a->next = list2;    \n    tail2->next = node_b;\n    return list1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given two linked lists:&nbsp;list1&nbsp;and&nbsp;list2&nbsp;of sizes&nbsp;n&nbsp;and&nbsp;m&nbsp;respectively. Remove&nbsp;list1&#8216;s nodes from the&nbsp;ath&nbsp;node to the&nbsp;bth&nbsp;node, and put&nbsp;list2&nbsp;in their place. The blue edges and nodes in the following&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[50],"tags":[295,83,177,248,670],"class_list":["post-7722","post","type-post","status-publish","format-standard","hentry","category-list","tag-dummy-head","tag-list","tag-medium","tag-prev","tag-tail","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7722","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=7722"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7722\/revisions"}],"predecessor-version":[{"id":7724,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7722\/revisions\/7724"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7722"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7722"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7722"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}