LeetCode: Insertion Sort List

Title:

Sort a linked list using insertion sort.

class Solution {
public:
    ListNode *insertionSortList(ListNode *head) {
        ListNode* sortedHead = new ListNode(0);
        while (head){
            ListNode* cur = sortedHead;
            ListNode* tmp = head->next;
            while (cur->next != NULL && cur->next->val < head->val){
                cur = cur->next;
            }
            head->next = cur->next;
            cur->next = head;
            head = tmp;
        }
        return sortedHead->next;
    }
};
原文地址:https://www.cnblogs.com/yxzfscg/p/4530001.html