首页 > 其他 > 详细

147. Insertion Sort List (List)

时间:2015-10-03 10:35:49      阅读:257      评论:0      收藏:0      [点我收藏+]

Sort a linked list using insertion sort.

class Solution {
public:
    ListNode *insertionSortList(ListNode *head) {
        if(!head || !head->next) return head;
        ListNode *tail =head->next;
        head->next = NULL;
        ListNode *current;
        ListNode *tmp;
        
        while(tail){
            if(tail->val < head->val){
                tmp = tail->next;
                tail->next = head;
                head = tail;
                tail = tmp;
                continue;
            } 
            
            current = head;
            while(current->next && tail->val >= current->next-> val)  current = current->next;
            tmp = tail->next;
            tail->next = current->next;
            current->next = tail;
            tail = tmp;
        }
        return head;
    }
};

 

147. Insertion Sort List (List)

原文:http://www.cnblogs.com/qionglouyuyu/p/4853065.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!