mark of my C++ solution

  • 2
    class Solution {
        ListNode* insertionSortList(ListNode* head) {
            //faked head node 
            ListNode* res = new ListNode(-1);
            ListNode* cur = res;
            //head is the current node we are dealing with
            while (head) {
                ListNode *next = head->next;
                cur = res;
                while (cur->next && cur->next->val <= head->val) {
                    cur = cur->next;
                //insert the head node into the list
                head->next = cur->next;
                cur->next = head;
                //move forward the head node
                head = next;
            return res->next;

  • 0

    Thanks for sharing! Nice one!

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.