My c code, how to improve, only beat 40%


  • 0
    X

    struct ListNode* insertionSortList(struct ListNode* head) {

       struct ListNode* sortedList;
       struct ListNode* org_p;
       struct ListNode* prev;
       struct ListNode* p;
    
    if(head == NULL||head->next == NULL) return head;
    
    sortedList = head;
    
    org_p=head->next;
    sortedList->next = NULL; 
    
    while(org_p!=NULL){
        p = sortedList;
    	if(org_p->val<=sortedList->val){
    		sortedList = org_p;
    		org_p = org_p->next;			
    		sortedList->next = p;
    	}
    	else{
    		while(p!=NULL && p->val<=org_p->val){
    			prev = p;
    			p=p->next;
    		}
    
    		prev->next = org_p;
    		org_p = org_p->next;
    		prev->next->next = p;
    	}
    }    
    return sortedList;
    

    }


Log in to reply
 

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