C++ solution using double point


  • 0
    L
     ListNode* insertionSortList(ListNode* head) {
        if (!head || !head->next) return head;
        ListNode *cur = head->next, **pre = &head;
        head->next = nullptr;
        while (cur){
            while (*pre && (*pre)->val <= cur->val) pre = &(*pre)->next;
            ListNode *tmp = cur->next;
            cur->next = *pre;
            *pre = cur;
            pre = &head;
            cur = tmp;
        }
        return head;
    }

Log in to reply
 

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