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

147. Insertion Sort List (List)

时间:2015-10-03 10:38:26      阅读:153      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!