{"id":8873,"date":"2021-11-28T14:10:04","date_gmt":"2021-11-28T22:10:04","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8873"},"modified":"2021-11-28T19:47:47","modified_gmt":"2021-11-29T03:47:47","slug":"leetcode-160-intersection-of-two-linked-lists","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/list\/leetcode-160-intersection-of-two-linked-lists\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 160. Intersection of Two Linked Lists"},"content":{"rendered":"\n<p>Given the heads of two singly linked-lists&nbsp;<code>headA<\/code>&nbsp;and&nbsp;<code>headB<\/code>, return&nbsp;<em>the node at which the two lists intersect<\/em>. If the two linked lists have no intersection at all, return&nbsp;<code>null<\/code>.<\/p>\n\n\n\n<p>For example, the following two linked lists begin to intersect at node&nbsp;<code>c1<\/code>:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/03\/05\/160_statement.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.<\/p>\n\n\n\n<p><strong>Note<\/strong>&nbsp;that the linked lists must&nbsp;<strong>retain their original structure<\/strong>&nbsp;after the function returns.<\/p>\n\n\n\n<p><strong>Custom Judge:<\/strong><\/p>\n\n\n\n<p>The inputs to the&nbsp;<strong>judge<\/strong>&nbsp;are given as follows (your program is&nbsp;<strong>not<\/strong>&nbsp;given these inputs):<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>intersectVal<\/code>&nbsp;&#8211; The value of the node where the intersection occurs. This is&nbsp;<code>0<\/code>&nbsp;if there is no intersected node.<\/li><li><code>listA<\/code>&nbsp;&#8211; The first linked list.<\/li><li><code>listB<\/code>&nbsp;&#8211; The second linked list.<\/li><li><code>skipA<\/code>&nbsp;&#8211; The number of nodes to skip ahead in&nbsp;<code>listA<\/code>&nbsp;(starting from the head) to get to the intersected node.<\/li><li><code>skipB<\/code>&nbsp;&#8211; The number of nodes to skip ahead in&nbsp;<code>listB<\/code>&nbsp;(starting from the head) to get to the intersected node.<\/li><\/ul>\n\n\n\n<p>The judge will then create the linked structure based on these inputs and pass the two heads,&nbsp;<code>headA<\/code>&nbsp;and&nbsp;<code>headB<\/code>&nbsp;to your program. If you correctly return the intersected node, then your solution will be&nbsp;<strong>accepted<\/strong>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image;crayon:false\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/03\/05\/160_example_1_1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3\n<strong>Output:<\/strong> Intersected at '8'\n<strong>Explanation:<\/strong> The intersected node's value is 8 (note that this must not be 0 if the two lists intersect).\nFrom the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,6,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image;crayon:false\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/03\/05\/160_example_2.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1\n<strong>Output:<\/strong> Intersected at '2'\n<strong>Explanation:<\/strong> The intersected node's value is 2 (note that this must not be 0 if the two lists intersect).\nFrom the head of A, it reads as [1,9,1,2,4]. From the head of B, it reads as [3,2,4]. There are 3 nodes before the intersected node in A; There are 1 node before the intersected node in B.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/03\/05\/160_example_3.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2\n<strong>Output:<\/strong> No intersection\n<strong>Explanation:<\/strong> From the head of A, it reads as [2,6,4]. From the head of B, it reads as [1,5]. Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be arbitrary values.\nExplanation: The two lists do not intersect, so return null.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The number of nodes of&nbsp;<code>listA<\/code>&nbsp;is in the&nbsp;<code>m<\/code>.<\/li><li>The number of nodes of&nbsp;<code>listB<\/code>&nbsp;is in the&nbsp;<code>n<\/code>.<\/li><li><code>0 &lt;= m, n &lt;= 3 * 10<sup>4<\/sup><\/code><\/li><li><code>1 &lt;= Node.val &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>0 &lt;= skipA &lt;= m<\/code><\/li><li><code>0 &lt;= skipB &lt;= n<\/code><\/li><li><code>intersectVal<\/code>&nbsp;is&nbsp;<code>0<\/code>&nbsp;if&nbsp;<code>listA<\/code>&nbsp;and&nbsp;<code>listB<\/code>&nbsp;do not intersect.<\/li><li><code>intersectVal == listA[skipA] == listB[skipB]<\/code>&nbsp;if&nbsp;<code>listA<\/code>&nbsp;and&nbsp;<code>listB<\/code>&nbsp;intersect.<\/li><\/ul>\n\n\n\n<p><strong>Follow up:<\/strong>&nbsp;Could you write a solution that runs in&nbsp;<code>O(n)<\/code>&nbsp;time and use only&nbsp;<code>O(1)<\/code>&nbsp;memory?<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Two Passes by swapping heads<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(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++\">\/\/ Author: Huahua\nclass Solution {\npublic:\n  ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n    if (!headA || !headB) return nullptr;\n    ListNode* a = headA;\n    ListNode* b = headB;\n    while (a != b) {\n      a = a ? a->next : headB;\n      b = b ? b->next : headA;\n    }\n    return a;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given the heads of two singly linked-lists&nbsp;headA&nbsp;and&nbsp;headB, return&nbsp;the node at which the two lists intersect. If the two linked lists have no intersection at all,&#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":[222,210,83],"class_list":["post-8873","post","type-post","status-publish","format-standard","hentry","category-list","tag-easy","tag-intersection","tag-list","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8873","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=8873"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8873\/revisions"}],"predecessor-version":[{"id":8900,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8873\/revisions\/8900"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8873"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8873"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}