My solution with HashMap in Java


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

Log in to reply
 

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