-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path160.java
More file actions
31 lines (31 loc) · 826 Bytes
/
160.java
File metadata and controls
31 lines (31 loc) · 826 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// 160. Intersection of Two Linked Lists
// https://leetcode.com/problems/intersection-of-two-linked-lists/
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
HashSet<ListNode> set = new HashSet();
ListNode iter1 = headA;
while(iter1 != null) {
set.add(iter1);
iter1 = iter1.next;
}
ListNode iter2 = headB;
while(iter2 != null) {
if (set.contains(iter2)) {
return iter2;
}
iter2 = iter2.next;
}
return null;
}
}