Skip to content

Commit a0439c5

Browse files
solves intersection of 2 linkedlists
1 parent 15f3562 commit a0439c5

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
| 141 | [Linked List Cycle](https://leetcode.com/problems/linked-list-cycle) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/LinkedListCycle.java) |
5050
| 155 | [Min Stack](https://leetcode.com/problems/min-stack) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/MinStack.java) |
5151
| 157 | [Read N Characters Given Read4](https://leetcode.com/problems/read-n-characters-given-read4) | Easy |
52-
| 160 | [Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists) | Easy |
52+
| 160 | [Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)]() |
5353
| 167 | [Two Sum II - Input Array is Sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted) | Easy |
5454
| 168 | [Excel Sheet Column Title](https://leetcode.com/problems/excel-sheet-column-title) | Easy |
5555
| 169 | [Majority Element](https://leetcode.com/problems/majority-element) | Easy |

src/IntersectionOf2LinkedLists.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import java.util.HashSet;
2+
import java.util.Set;
3+
4+
public class IntersectionOf2LinkedLists {
5+
public static class ListNode {
6+
int val;
7+
ListNode next;
8+
ListNode(int x) {
9+
val = x;
10+
next = null;
11+
}
12+
}
13+
14+
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
15+
Set<ListNode> aNodes = new HashSet<>();
16+
while (headA != null) {
17+
aNodes.add(headA);
18+
headA = headA.next;
19+
}
20+
while (headB != null) {
21+
if (aNodes.contains(headB)) {
22+
return headB;
23+
}
24+
headB = headB.next;
25+
}
26+
return null;
27+
}
28+
}

0 commit comments

Comments
 (0)