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

代码

/*
 * @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
posted @ 2020-08-08 14:01  景云ⁿ  阅读(67)  评论(0编辑  收藏  举报