Check if two linked lists merge. If so, where?

后端 未结 26 1432
陌清茗
陌清茗 2020-12-07 06:43

This question may be old, but I couldn\'t think of an answer.

Say, there are two lists of different lengths, merging at a point; how do we know wher

26条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-07 07:37

    int FindMergeNode(Node *headA, Node *headB)
    {
        Node *tempB=new Node;
        tempB=headB;
       while(headA->next!=NULL)
           {
           while(tempB->next!=NULL)
               {
               if(tempB==headA)
                   return tempB->data;
               tempB=tempB->next;
           }
           headA=headA->next;
           tempB=headB;
       }
        return headA->data;
    }
    

提交回复
热议问题