• + 164 comments

    I think this solution I found here is pretty genius. You just sinc the two list iterators by connecting the ends of each list to the beginning of the next list:

    int FindMergeNode(Node headA, Node headB) {
        Node currentA = headA;
        Node currentB = headB;
    
        //Do till the two nodes are the same
        while(currentA != currentB){
            //If you reached the end of one list start at the beginning of the other one
            //currentA
            if(currentA.next == null){
                currentA = headB;
            }else{
                currentA = currentA.next;
            }
            //currentB
            if(currentB.next == null){
                currentB = headA;
            }else{
                currentB = currentB.next;
            }
        }
        return currentB.data;
    }