54ms C++ solution


  • 0
    S
    class Solution {
    public:
        ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
            
            ListNode *curr1 = headA, *curr2 = headB;
            
            
            while(curr1 && curr2){
                
                if(curr1 == curr2) return curr2;
                
                curr1 = curr1->next;
                curr2 = curr2->next;
                
                if(!curr1 && !curr2) return NULL;
                
                if(curr1 == NULL) curr1 = headB;
                if(curr2 == NULL) curr2 = headA;
            }
            return NULL;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.