Even more simple solution in JavaScript


  • 0
    N
    var getIntersectionNode = function(headA, headB) {
       let set = new Set();
       while (headA || headB) {  
           if (headA) {
               if (set.has(headA.val)) {
                   return headA;
               }
               set.add(headA.val);
               headA = headA.next;
           }
           if (headB) {
               if (set.has(headB.val)) {
                   return headB;
                }
               set.add(headB.val);
               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.