首页 > 编程语言 > 详细

147 Insertion Sort List 链表插入排序

时间:2018-04-06 14:51:07      阅读:163      评论:0      收藏:0      [点我收藏+]

用插入排序对链表进行排序。

详见:https://leetcode.com/problems/insertion-sort-list/description/

/**
 * 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* helper=new ListNode(-1);
        ListNode* cur=helper;
        while(head)
        {
            ListNode* next=head->next;
            cur=helper;
            while(cur->next&&cur->next->val<=head->val)
            {
                cur=cur->next;
            }
            head->next=cur->next;
            cur->next=head;
            head=next;
        }
        return helper->next;
    }
};

 

147 Insertion Sort List 链表插入排序

原文:https://www.cnblogs.com/xidian2014/p/8727445.html

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