My c++ solution


  • 0
    N
    if (head == NULL||head->next==NULL)
    	return head;
    int miximal = 10000;
    ListNode *h = head;
    while (h)
    {
    	if (h->val < miximal)
    		miximal = h->val;
    	h = h->next;
    }
    ListNode prehead(miximal);	
    while (head)
    {
    	ListNode *cur = &prehead;
    	while (cur->next)
    	{
    		if (head->val >= cur->val&&head->val<=cur->next->val)
    		{
    			ListNode *next = cur->next;
    			cur->next = new ListNode(head->val);
    			cur->next->next = next;
    			break;
    		}
    		cur = cur->next;
    	}
    	if (cur->next == NULL)
    	{
    		if (head->val >= cur->val)
    			cur->next = new ListNode(head->val);
    	}
    	head = head->next;
    }
    return  prehead.next;

Log in to reply
 

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