Online Judge Solutions

Sunday, November 2, 2014

Insertion Sort List

 
ListNode *insertionSortList(ListNode *head) {
        if (!head) return head;
        ListNode dummy(numeric_limits<int>::min());
       
        while(head) {
            ListNode *p = &dummy;   
            while(p->next && p->next->val <= head->val)
              p = p->next;
           
            ListNode *headNext = head->next;
            head->next = p->next;
            p->next = head;
            head = headNext;
        }
        return dummy.next;
    }

No comments:

Post a Comment