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

LeetCode: Insertion Sort List

时间:2015-05-26 12:14:53      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:

Title:

Sort a linked list using insertion sort.

 

class Solution {
public:
    ListNode *insertionSortList(ListNode *head) {
        ListNode* sortedHead = new ListNode(0);
        while (head){
            ListNode* cur = sortedHead;
            ListNode* tmp = head->next;
            while (cur->next != NULL && cur->next->val < head->val){
                cur = cur->next;
            }
            head->next = cur->next;
            cur->next = head;
            head = tmp;
        }
        return sortedHead->next;
    }
};

 

LeetCode: Insertion Sort List

标签:

原文地址:http://www.cnblogs.com/yxzfscg/p/4530001.html

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