Simple JAVA HashSet


  • 0
    H
    public class Solution {
        public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
            HashSet<Integer> s1=new HashSet<Integer>();
            while(headA!=null){s1.add(headA.val);headA=headA.next;}
            while(headB!=null){
                if(!s1.add(headB.val)){return headB;}
                headB=headB.next;
            }
            return null;     
            }
           
    }
    

Log in to reply
 

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