Java solution with hashmap


  • 0
    P

    public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
    HashMap<ListNode,Integer> map = new HashMap<ListNode,Integer>();
    ListNode root = null;
    root = headA;
    while(root!=null){
    map.put(root,1);
    root = root.next;
    }
    root = headB;
    while(root!=null){
    if(map.containsKey(root)){
    return root;
    }
    root = root.next;
    }
    return null;
    }
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.