• + 0 comments

    The Constraint LIES:

    head1 != head2

    that is a lie. i fixed my code by allowing search for head it answered correctly.

    c++ solution:

    int findMergeNode(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2) {
        auto first = head1;
        auto second = head2;
        while (first != second) {
            first = (first->next) ? first->next : head2;
            second = (second->next) ? second->next : head1;
        }
        // no merge, should never happend
        if (first == nullptr)
            throw -1;
        return first->data;
    }