leetcode——Insertion Sort List 对链表进行插入排序(AC)

Sort a linked list using insertion sort.

class Solution {
public:
    ListNode *insertionSortList(ListNode *head) {
        if(head == NULL || head->next == NULL)
            return head;
        ListNode *result;
        result->val = INT_MIN;
        result->next = NULL;
        ListNode *cur=head,*pos,*pre;
        while(cur!=NULL)
        {
            pos = result->next;
            pre = result;
            while(pos != NULL && pos->val <= cur->val)
            {
                pre = pos;
                pos = pos->next;
            }
            ListNode *temp = cur->next;
            pre->next = cur;
            cur->next = pos;
            cur = temp;
        }
        return result->next;
    }
};


posted on 2017-05-30 10:03  blfbuaa  阅读(116)  评论(0编辑  收藏  举报