A simple C solution[Accepted]


  • 0
    B
    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     struct ListNode *next;
     * };
     */
    struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
        if(headA==NULL||headB==NULL){return NULL;}
        struct ListNode *tempA=headA;
        struct ListNode *tempB=headB;
        for(int i=0;i<3;i++){
            while(tempA!=NULL&&tempB!=NULL){
                if(tempA==tempB){return tempA;}
                tempA=tempA->next;
                tempB=tempB->next;  
            }
            if(tempA==NULL){tempA=headB;}
            if(tempB==NULL){tempB=headA;}
        }
        return NULL;
    }
    

Log in to reply
 

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