Can someone tell me where it is wrong?


  • 0
    L

    '''
    /**

    • Definition for singly-linked list.

    • struct ListNode {

    • int val;
      
    • struct ListNode *next;
      
    • };
      /
      struct ListNode
      mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {
      struct ListNode *temp,*p2,*f,*p;

      //若l1,l2都不存在
      if(!l1&&!l2) return 0;

      //若l1不存在
      if(!l1)
      return l2;

      //若l2不存在
      if(!l2)
      return l1;

      //若l2,l1都存在
      if(l1&&l2)
      {
      struct ListNode* head;
      head = (struct ListNode*)malloc(sizeof(struct ListNode));
      head->next = l1;

       f = head;
       p2 = l2;//记得赋初值
       while(p2)
       {
           int flag=0;
           while(f->next)//75-86行代码要认真理解好
           {
               if((p2->val)<=(f->next->val))//P2始终指向第一个元素
               {
                   temp=p2;
                   p2=p2->next;//这两句顺序不能颠倒,把p2->next赋值给p2为了方便后面对p2的遍历
                   temp->next = f->next;
                   f->next = temp;
                   flag=1;
                   break;
               }
               f = f->next;
           }
           if(flag==0)
           {
               f->next = p2;
               break;
           }
       }
      

      }
      return l1;
      }
      '''


Log in to reply
 

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