147. Insertion Sort List [LeetCode]

版权声明:QQ:872289455. WeChat:luw9527. https://blog.csdn.net/MC_007/article/details/80871090

147Insertion Sort List

链表排序,插入排序


class Solution {
public:
    ListNode * insertionSortList(ListNode * head) {
         if (head == nullptr || head-> next == nullptr) return head;
         //创建一个系统最小节点
        ListNode dummy(INT_MIN);
         for (ListNode *cur = head; cur;) {
             auto pos = findInsertPos( &dummy, cur-> val);
             //把cur节点插入pos节点之后
            ListNode *tmp = cur-> next;
            cur-> next = pos-> next;
            pos-> next = cur;
            cur = tmp;
        }
         return dummy. next;
    }
    ListNode * findInsertPos(ListNode *head, int x) {
        ListNode *pre = nullptr;
         for (ListNode *cur = head; cur &&cur-> val <= x; pre = cur, cur = cur-> next);
         return pre;
    }
};

猜你喜欢

转载自blog.csdn.net/MC_007/article/details/80871090