Skip to content

Commit b74463b

Browse files
Create README - LeetHub
1 parent d54fd3c commit b74463b

File tree

1 file changed

+64
-0
lines changed
  • 0160-intersection-of-two-linked-lists

1 file changed

+64
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<h2><a href="https://leetcode.com/problems/intersection-of-two-linked-lists/">160. Intersection of Two Linked Lists</a></h2><h3>Easy</h3><hr><div><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>
2+
3+
<p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p>
4+
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;">
5+
<p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p>
6+
7+
<p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p>
8+
9+
<p><strong>Custom Judge:</strong></p>
10+
11+
<p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p>
12+
13+
<ul>
14+
<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>
15+
<li><code>listA</code> - The first linked list.</li>
16+
<li><code>listB</code> - The second linked list.</li>
17+
<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>
18+
<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>
19+
</ul>
20+
21+
<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>
22+
23+
<p>&nbsp;</p>
24+
<p><strong class="example">Example 1:</strong></p>
25+
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;">
26+
<pre><strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3
27+
<strong>Output:</strong> Intersected at '8'
28+
<strong>Explanation:</strong> The intersected node's value is 8 (note that this must not be 0 if the two lists intersect).
29+
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.
30+
- Note that the intersected node's value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> node in B) are different node references. In other words, they point to two different locations in memory, while the nodes with value 8 in A and B (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory.
31+
</pre>
32+
33+
<p><strong class="example">Example 2:</strong></p>
34+
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;">
35+
<pre><strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
36+
<strong>Output:</strong> Intersected at '2'
37+
<strong>Explanation:</strong> The intersected node's value is 2 (note that this must not be 0 if the two lists intersect).
38+
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.
39+
</pre>
40+
41+
<p><strong class="example">Example 3:</strong></p>
42+
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;">
43+
<pre><strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
44+
<strong>Output:</strong> No intersection
45+
<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.
46+
Explanation: The two lists do not intersect, so return null.
47+
</pre>
48+
49+
<p>&nbsp;</p>
50+
<p><strong>Constraints:</strong></p>
51+
52+
<ul>
53+
<li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li>
54+
<li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li>
55+
<li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li>
56+
<li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li>
57+
<li><code>0 &lt;= skipA &lt;&nbsp;m</code></li>
58+
<li><code>0 &lt;= skipB &lt;&nbsp;n</code></li>
59+
<li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li>
60+
<li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li>
61+
</ul>
62+
63+
<p>&nbsp;</p>
64+
<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?</div>

0 commit comments

Comments
 (0)