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

Insertion Sort List -- leetcode

时间:2015-06-07 17:34:51      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:leetcode   插入排序   

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

标签:leetcode   插入排序   

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

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