码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-07-15 12:22:53      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:插入排序   链表   

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;
    }
};


leetcode——Insertion Sort List 对链表进行插入排序(AC),布布扣,bubuko.com

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

标签:插入排序   链表   

原文地址:http://blog.csdn.net/dalongyes/article/details/37774007

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!