Sort by

recency

|

1214 Discussions

|

  • + 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;   
        }
    
  • + 0 comments

    OPTIMIZED APPROACH

     static int length(SinglyLinkedListNode temp)
         {
            int len =0;
            while(temp != null)
            {
                len++;
                temp = temp.next;
            }
            return len;
         }
        static int findMergeNode(SinglyLinkedListNode head1, SinglyLinkedListNode head2) {
            
            int len1 = length(head1);
            int len2 = length(head2);
            int mov = 0;
            SinglyLinkedListNode temp = null;
            boolean head1_moved = false;
            boolean head2_moved = false;
            if(len1 > len2)
            {
                mov = len1 - len2;
                temp = head1;
                while(mov-- > 0)
                {
                    temp = temp.next;
                }
                head1_moved = true;
            }
            else if(len1 <= len2)
            {
                mov = len2 - len1;
                temp = head2;
                while(mov-- > 0)
                {
                    temp = temp.next;
                }
                head2_moved = true;
            }
            
            if(head1_moved)
            {
                SinglyLinkedListNode curr = head2;
                while(temp != null)
                {
                    if(curr == temp)
                    {
                        return curr.data;
                    }
                    temp = temp.next;
                    curr = curr.next;
                }
            }
            else if(head2_moved){
                SinglyLinkedListNode curr = head1;
                while(temp != null)
                {
                    if(curr == temp)
                    {
                        return curr.data;
                    }
                    temp = temp.next;
                    curr = curr.next;
                }
            }
            
            
            return -1;
    
        }
    
  • + 0 comments

    BRUTE FORCE Approach

     static int findMergeNode(SinglyLinkedListNode head1, SinglyLinkedListNode head2) {
    
            HashMap<SinglyLinkedListNode, Integer> map = new HashMap<>();
            SinglyLinkedListNode temp1 = head1;
            SinglyLinkedListNode temp2 = head2;
            
            while(temp1 != null)
            {
                    map.put(temp1, 1);
                    temp1 = temp1.next;
                
            }
            while(temp2 != null)
            {
                if(map.containsKey(temp2))
                {
                    return temp2.data;
                }
                else{
                    map.put(temp2, 1);
                    temp2 = temp2.next;
                }
            }
            return -1;
    				
        }
    
  • + 0 comments

    can you help me to integrate this script into my business setup website , i want to use this on my wordpress website

  • + 0 comments

    solution with JS

    function findMergeNode(headA, headB) {
       let current1 = headA;
        let current2 = headB;
    
        let length1 = 0;
        let length2 = 0;
    
        while (current1) {
            length1++;
            current1 = current1.next;
        }
    
        while (current2) {
            length2++;
            current2 = current2.next;
        }
    
        current1 = headA;
        current2 = headB;
    
        if (length1 > length2) {
            for (let i = 0; i < length1 - length2; i++) {
                current1 = current1.next;
            }
        } else if (length2 > length1) {
            for (let i = 0; i < length2 - length1; i++) {
                current2 = current2.next;
            }
        }
    
        while (current1 && current2) {
            if (current1 === current2) {
                return current1.data; 
            }
            current1 = current1.next;
            current2 = current2.next;
        }
    
        return null; 
    }