s4553711
4/4/2017 - 2:01 PM

147.cpp

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* insertionSortList(ListNode* head) {
        ListNode* dummy = new ListNode(0);
        while(head != NULL) {
            ListNode* node = dummy;
            while(node->next != NULL && node->next->val < head->val) {
                node = node->next;
            }
            
            ListNode* tmp = head->next;
            head->next = node->next;
            node->next = head;
            head = tmp;
        }
        return dummy->next;
    }
};