首页 > 编程语言 > 详细

leetcode-----147. 对链表进行插入排序

时间:2020-08-08 17:56:12      阅读:70      评论:0      收藏:0      [点我收藏+]

代码

/*
 * @lc app=leetcode.cn id=147 lang=cpp
 *
 * [147] 对链表进行插入排序
 */

// @lc code=start
/**
 * 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) {
        auto dummy = new ListNode(-1);
        for (auto p = head; p; ) {
            auto cur = dummy, next = p->next;
            while (cur->next && cur->next->val <= p->val) cur  = cur->next;
            p->next = cur->next;
            cur->next = p;
            p = next;
        }
        return dummy->next;
    }
};
// @lc code=end

leetcode-----147. 对链表进行插入排序

原文:https://www.cnblogs.com/clown9804/p/13457569.html

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