Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit be93f2f

Browse files
committed
Added README.md file for Intersection of Two Linked Lists
1 parent 7b9285c commit be93f2f

File tree

1 file changed

+67
-0
lines changed
  • 160-intersection-of-two-linked-lists

1 file changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<h2><a href="https://leetcode.com/problems/intersection-of-two-linked-lists">Intersection of Two Linked Lists</a></h2> <img src='https://img.shields.io/badge/Difficulty-Easy-brightgreen' alt='Difficulty: Easy' /><hr><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>
27+
<strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3
28+
<strong>Output:</strong> Intersected at &#39;8&#39;
29+
<strong>Explanation:</strong> The intersected node&#39;s value is 8 (note that this must not be 0 if the two lists intersect).
30+
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.
31+
- Note that the intersected node&#39;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.
32+
</pre>
33+
34+
<p><strong class="example">Example 2:</strong></p>
35+
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" />
36+
<pre>
37+
<strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
38+
<strong>Output:</strong> Intersected at &#39;2&#39;
39+
<strong>Explanation:</strong> The intersected node&#39;s value is 2 (note that this must not be 0 if the two lists intersect).
40+
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.
41+
</pre>
42+
43+
<p><strong class="example">Example 3:</strong></p>
44+
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" />
45+
<pre>
46+
<strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
47+
<strong>Output:</strong> No intersection
48+
<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.
49+
Explanation: The two lists do not intersect, so return null.
50+
</pre>
51+
52+
<p>&nbsp;</p>
53+
<p><strong>Constraints:</strong></p>
54+
55+
<ul>
56+
<li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li>
57+
<li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li>
58+
<li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li>
59+
<li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li>
60+
<li><code>0 &lt;= skipA &lt;&nbsp;m</code></li>
61+
<li><code>0 &lt;= skipB &lt;&nbsp;n</code></li>
62+
<li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li>
63+
<li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li>
64+
</ul>
65+
66+
<p>&nbsp;</p>
67+
<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?

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /