mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-11 02:58:13 +08:00
66 lines
4.3 KiB
HTML
66 lines
4.3 KiB
HTML
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p>
|
|
|
|
<p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p>
|
|
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" />
|
|
<p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p>
|
|
|
|
<p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p>
|
|
|
|
<p><strong>Custom Judge:</strong></p>
|
|
|
|
<p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p>
|
|
|
|
<ul>
|
|
<li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li>
|
|
<li><code>listA</code> - The first linked list.</li>
|
|
<li><code>listB</code> - The second linked list.</li>
|
|
<li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li>
|
|
<li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li>
|
|
</ul>
|
|
|
|
<p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p>
|
|
|
|
<p> </p>
|
|
<p><strong>Example 1:</strong></p>
|
|
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" />
|
|
<pre>
|
|
<strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3
|
|
<strong>Output:</strong> Intersected at '8'
|
|
<strong>Explanation:</strong> The intersected node's value is 8 (note that this must not be 0 if the two lists intersect).
|
|
From 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.
|
|
</pre>
|
|
|
|
<p><strong>Example 2:</strong></p>
|
|
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" />
|
|
<pre>
|
|
<strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
|
|
<strong>Output:</strong> Intersected at '2'
|
|
<strong>Explanation:</strong> The intersected node's value is 2 (note that this must not be 0 if the two lists intersect).
|
|
From 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.
|
|
</pre>
|
|
|
|
<p><strong>Example 3:</strong></p>
|
|
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" />
|
|
<pre>
|
|
<strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
|
|
<strong>Output:</strong> No intersection
|
|
<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.
|
|
Explanation: The two lists do not intersect, so return null.
|
|
</pre>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li>
|
|
<li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li>
|
|
<li><code>1 <= m, n <= 3 * 10<sup>4</sup></code></li>
|
|
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
|
|
<li><code>0 <= skipA < m</code></li>
|
|
<li><code>0 <= skipB < n</code></li>
|
|
<li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li>
|
|
<li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li>
|
|
</ul>
|
|
|
|
<p> </p>
|
|
<strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory? |