• + 0 comments

    The key idea to check the the same object is exist in the set

    static int findMergeNode(SinglyLinkedListNode head1, SinglyLinkedListNode head2) {
            Set<SinglyLinkedListNode> nodeSet = new HashSet<>();
            
            SinglyLinkedListNode currh1 = head1;
            SinglyLinkedListNode currh2 = head2;
            while(currh1 != null){
                nodeSet.add(currh1);
                currh1 = currh1.next;
            }
            
            while(currh2 != null){
                if(nodeSet.contains(currh2)){
                    return currh2.data;
                }
                currh2 = currh2.next;
            }
            
            return -1;   
        }