@violangreg/

Leetcode Java

Java

No description

fork
loading
Files
  • Main.java
  • ListNode.java
Main.java
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
32
33
34
35
36
37
38
import java.util.*;

class Main {
  public static void main(String[] args) {
    ListNode a = new ListNode(1);
    ListNode b = new ListNode(2);
    ListNode c = new ListNode(3);
    a.next = b;
    b.next = c;

    ListNode d = new ListNode(5);
    ListNode f = new ListNode(7);
    d.next = f;
    f.next = b;
    b.next = c;

    System.out.println(getIntersectionNode(a, d).val);
  }    
  
public static ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        HashMap<ListNode,Integer> map = new HashMap<ListNode,Integer>();
        ListNode current1 = headA;
        ListNode current2 = headB;
        while(current1!=null){
                map.put(current1,current1.val);
                current1 = current1.next;
        }
        
        while(current2!=null){
            if(map.containsKey(current2)){
                return current2;
            }
            current2 = current2.next;
        }
        
        return null;
    }
}