Insertion Sort List -- leetcode

时间:2015-06-07 17:31:38   收藏:0   阅读:227

Sort a linked list using insertion sort.

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* insertionSortList(ListNode* head) {
        ListNode fake(0);
        while (head) {
            ListNode *runner = &fake;
            while (runner->next && runner->next->val < head->val)
                runner = runner->next;
            
            ListNode *bak = head->next;
            head->next = runner->next;
            runner->next = head;
            head = bak;
        }
        return fake.next;
    }
};


原文:http://blog.csdn.net/elton_xiao/article/details/46401459

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!