What's wrong with my C solution?


  • 0
    H

    `

    struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB)
    {

    struct ListNode *A=headA;
    
    struct ListNode *B=headB;
    
    int la=0,lb=0;
    while(A!=NULL)
    {A=A->next;
        la++;}
    while(B!=NULL)
    {B=B->next;
        lb++;}
    A=headA;
    B=headB;
    if(la>lb)
    {for(int i=0;i<la-lb;i++)
        A=A->next;
    for(int j=0;j<lb&&A!=B;j++)
        {A=A->next;B=B->next;}
    }
    else if(la<lb)
    {for(int k=0;k<lb-la;k++)
        B=B->next;
    for(int l=0;l<la&&A!=B;l++)
        {A=A->next;B=B->next;}
    }
    else (la==lb)
    {   if(A=B&&la!=0)  return A;
        for(int m=0;m<la&&A!=B;m++)
        {A=A->next; B=B->next;}
    }
    if(A==NULL||B==NULL)
    return NULL;
    else 
    return A;
    

    }

    `


  • 0
    D
    else **(la==lb)**
    {   if(**A=B**&&la!=0)  return A;
        for(int m=0;m<la&&A!=B;m++)
        {A=A->next; B=B->next;}
    }
    
    1. (la == lb) should not be there. Is it actually comments?
    2. should be:
      if (A**==**B && la !=0) return A;

Log in to reply
 

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