Saturday, November 29, 2014

Insertion Sort List

    ListNode *insertionSortList(ListNode *head) {
        if(!head) return NULL;
        ListNode*dummy=new ListNode(0);
        dummy->next=head;
        while(head->next){
            ListNode*tmp=head->next;
            if(tmp->val<head->val){
                head->next=tmp->next;
                ListNode*pre=dummy;
                while(pre->next->val<=tmp->val)
                    pre=pre->next;
                tmp->next=pre->next;
                pre->next=tmp;
            }
            else
                head=head->next;
        }
        return dummy->next;
    }

No comments:

Post a Comment