首页 > 其他 > 详细

Insertion Sort List -- leetcode

时间:2015-06-07 17:31:38      阅读:225      评论:0      收藏:0      [点我收藏+]

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


Insertion Sort List -- leetcode

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

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