Java with HashSet and easy idea


  • 1
    Z
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
            if(headA == null || headB == null)return null;
            HashSet<ListNode> set = new HashSet<>();
            while(headA != null){
                set.add(headA);
                headA = headA.next;
            }
            while(!set.contains(headB) && headB != null){
                headB = headB.next;
            }
            if(headB != null)return headB;
            return null;
        }
    

  • 0
    P

    Really simple and really nice. Thank you.


Log in to reply
 

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