Skip to content

Commit c8c38ad

Browse files
committed
Added 160
1 parent 356b4b4 commit c8c38ad

File tree

1 file changed

+213
-0
lines changed

1 file changed

+213
-0
lines changed
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
---
2+
id: intersection-of-two-linked-list.
3+
title: Intersection of Two Linked Lists
4+
sidebar_label: 0160-Intersection of Two Linked Lists
5+
tags:
6+
- Linked List
7+
- Leet code
8+
description: "Solution to leetocde 160"
9+
---
10+
11+
### Problem Description
12+
13+
Given the heads of two singly linked-lists headA and headB, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null.
14+
15+
For example, the following two linked lists begin to intersect at node c1:
16+
17+
The test cases are generated such that there are no cycles anywhere in the entire linked structure.
18+
19+
Note that the linked lists must retain their original structure after the function returns.
20+
21+
**Custom Judge:**
22+
23+
The inputs to the judge are given as follows (your program is not given these inputs):
24+
25+
- intersectVal - The value of the node where the intersection occurs. This is 0 if there is no intersected node.
26+
- listA - The first linked list.
27+
- listB - The second linked list.
28+
- skipA - The number of nodes to skip ahead in listA (starting from the head) to get to the intersected node.
29+
- skipB - The number of nodes to skip ahead in listB (starting from the head) to get to the intersected node.
30+
- The judge will then create the linked structure based on these inputs and pass the two heads, headA and headB to your program. If you correctly return the intersected node, then your solution will be accepted.
31+
32+
**Example 1:**
33+
34+
```
35+
Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3
36+
Output: Intersected at '8'
37+
Explanation: The intersected node's value is 8 (note that this must not be 0 if the two lists intersect).
38+
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.
39+
40+
- Note that the intersected node's value is not 1 because the nodes with value 1 in A and B (2nd node in A and 3rd 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 (3rd node in A and 4th node in B) point to the same location in memory.
41+
```
42+
43+
**Example 2:**
44+
45+
```
46+
Input: intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
47+
Output: Intersected at '2'
48+
Explanation: The intersected node's value is 2 (note that this must not be 0 if the two lists intersect).
49+
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.
50+
```
51+
52+
**Example 3:**
53+
54+
```
55+
Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
56+
Output: No intersection
57+
Explanation: 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.
58+
Explanation: The two lists do not intersect, so return null.
59+
```
60+
61+
### Constraints:
62+
63+
- The number of nodes of listA is in the m.
64+
- The number of nodes of listB is in the n.
65+
- $1 <= m, n <= 3*10^4$
66+
- $1 <= Node.val <= 10^5$
67+
- $0 <= skipA < m$
68+
- $0 <= skipB < n$
69+
- $intersectVal is 0 if listA and listB do not intersect.$
70+
- $intersectVal == listA[skipA] == listB[skipB] if listA and listB intersect.$
71+
72+
### Algorithm
73+
74+
The algorithm to find the intersection of two linked lists uses the following steps:
75+
76+
1. **Traverse the First List**:
77+
78+
- Store each node in a hash map (or set).
79+
80+
2. **Traverse the Second List**:
81+
- Check if any node is already present in the hash map (or set). If found, that node is the intersection.
82+
83+
### C++ Implementation
84+
85+
```cpp
86+
#include <unordered_map>
87+
88+
// Definition for singly-linked list.
89+
struct ListNode {
90+
int val;
91+
ListNode *next;
92+
ListNode(int x) : val(x), next(nullptr) {}
93+
};
94+
95+
class Solution {
96+
public:
97+
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
98+
std::unordered_map<ListNode*, int> mpp;
99+
100+
// Traverse list A and store each node in the map
101+
for (ListNode *p = headA; p != nullptr; p = p->next) {
102+
mpp[p] = p->val;
103+
}
104+
105+
// Traverse list B and check if any node is in the map
106+
for (ListNode *p = headB; p != nullptr; p = p->next) {
107+
if (mpp.find(p) != mpp.end()) {
108+
return p;
109+
}
110+
}
111+
112+
return nullptr;
113+
}
114+
};
115+
```
116+
117+
### Python Implementation
118+
119+
```python
120+
class ListNode:
121+
def __init__(self, x):
122+
self.val = x
123+
self.next = None
124+
125+
class Solution:
126+
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
127+
node_set = set()
128+
129+
# Traverse list A and store each node in the set
130+
p = headA
131+
while p:
132+
node_set.add(p)
133+
p = p.next
134+
135+
# Traverse list B and check if any node is in the set
136+
p = headB
137+
while p:
138+
if p in node_set:
139+
return p
140+
p = p.next
141+
142+
return None
143+
```
144+
145+
### Java Implementation
146+
147+
```java
148+
import java.util.HashSet;
149+
150+
class ListNode {
151+
int val;
152+
ListNode next;
153+
ListNode(int x) {
154+
val = x;
155+
next = null;
156+
}
157+
}
158+
159+
public class Solution {
160+
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
161+
HashSet<ListNode> nodeSet = new HashSet<>();
162+
163+
// Traverse list A and store each node in the set
164+
ListNode p = headA;
165+
while (p != null) {
166+
nodeSet.add(p);
167+
p = p.next;
168+
}
169+
170+
// Traverse list B and check if any node is in the set
171+
p = headB;
172+
while (p != null) {
173+
if (nodeSet.contains(p)) {
174+
return p;
175+
}
176+
p = p.next;
177+
}
178+
179+
return null;
180+
}
181+
}
182+
```
183+
184+
### JavaScript Implementation
185+
186+
```javascript
187+
function ListNode(val) {
188+
this.val = val;
189+
this.next = null;
190+
}
191+
192+
var getIntersectionNode = function (headA, headB) {
193+
let nodeSet = new Set();
194+
195+
// Traverse list A and store each node in the set
196+
let p = headA;
197+
while (p !== null) {
198+
nodeSet.add(p);
199+
p = p.next;
200+
}
201+
202+
// Traverse list B and check if any node is in the set
203+
p = headB;
204+
while (p !== null) {
205+
if (nodeSet.has(p)) {
206+
return p;
207+
}
208+
p = p.next;
209+
}
210+
211+
return null;
212+
};
213+
```

0 commit comments

Comments
 (0)