Simple c solution


  • -1
    B

    /**

    • Definition for singly-linked list.
    • struct ListNode {
    • int val;
      
    • struct ListNode *next;
      
    • };
      */
      struct ListNode *getIntersectionNode(struct ListNode *heada, struct ListNode *headb) {
      struct ListNode *temp1=heada,*temp2=headb;
      int i=0,j=0,d,l;
      while(temp1){
      temp1=temp1->next;
      i++;
      }
      while(temp2){
      temp2=temp2->next;
      j++;
      }
      if(i>j)
      d=i-j;
      else
      d=j-i;
      for(l=0;l<d;l++)
      {
      if(i>j)
      heada=heada->next;
      else
      headb=headb->next;
      }
      while(heada&&headb){
      if(heada==headb)
      return heada;
      heada=heada->next;
      headb=headb->next;
      }
      return NULL;

    }


Log in to reply
 

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