//Move cur1 (cur2) forward from headA (headB) and loop back to headB (headA), 
//eventually cur1 and cur2 will meet at the intersection point or nullptr.

ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
    ListNode *cur1 = headA, *cur2 = headB;
    while(cur1 != cur2){
        cur1 = cur1?cur1->next:headB;
        cur2 = cur2?cur2->next:headA;
    }
    return cur1;
}

results matching ""

    No results matching ""