//对链表进行插入排序。 
//
// 
//插入排序的动画演示如上。从第一个元素开始,该链表可以被认为已经部分排序(用黑色表示)。 
//每次迭代时,从输入数据中移除一个元素(用红色表示),并原地将其插入到已排好序的链表中。 
//
// 
//
// 插入排序算法: 
//
// 
// 插入排序是迭代的,每次只移动一个元素,直到所有元素可以形成一个有序的输出列表。 
// 每次迭代中,插入排序只从输入数据中移除一个待排序的元素,找到它在序列中适当的位置,并将其插入。 
// 重复直到所有输入数据插入完为止。 
// 
//
// 
//
// 示例 1: 
//
// 输入: 4->2->1->3
//输出: 1->2->3->4
// 
//
// 示例 2: 
//
// 输入: -1->5->3->4->0
//输出: -1->0->3->4->5
// 
// Related Topics 排序 链表 
// 👍 359 👎 0

/*
* 147 对链表进行插入排序
* 2021-02-28 12:59:22
* @author oxygenbytes
*/ 
#include "leetcode.h" 
//leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* insertionSortList(ListNode* head) {
        if(!head || !head->next) return head;

        auto dummy = new ListNode(-1); // 遍历n次,每次找到目标节点,并更新cur所在链表
        auto pre = head;


        while(pre){
            auto ptr = pre->next;
            auto cur = dummy;

            while(cur->next && cur->next->val <= pre->val){
                cur = cur->next;
            }
            pre->next = cur->next;
            cur->next = pre;
            pre = ptr;
        }

        return dummy->next;
    }
};
//leetcode submit region end(Prohibit modification and deletion)