Runtime error/time limit exceed when it comes to the situation of input{1,1}


  • 0
    L
    class Solution {
    public:
        ListNode *insertionSortList(ListNode *head) {
        
              if(!head||!head->next) return head;//注意点1
              ListNode *InsertNode=head->next;
              ListNode *pre=head,*pNode=head;
              while(InsertNode!=NULL){
                  while(!pNode&&pNode->val<=InsertNode->val)//找到第一个要插入的节点大pNode
                  {
                      pre=pNode;
                      pNode=pNode->next;
                  }
                  if(pNode==NULL) return head;
                  ListNode *Insert_Next=InsertNode->next;
                  //注意点2
                  if(pNode==head)//如果比头结点小,即插入到头前面
                  { 
                      
                      InsertNode->next=head;
                      head=InsertNode;
                      InsertNode=Insert_Next;
                  }
                  else
                  {
                  InsertNode->next=pNode;
                  pre->next=InsertNode;
                  InsertNode=Insert_Next;
                  }
                  
              //    InsertNode=InsertNode->next;
             
              }
                 return head;
            
        }
    };

Log in to reply
 

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