Clean 10 lines C++ recursive solution ( Does this count as insertion sort? )

  • 0

    I think the algorithm is the same as the iterative way, but only sort the largest (right) numbers first.

    ListNode* insertionSortList(ListNode* head) {
        if (!head)
            return NULL;
        head->next = insertionSortList(head->next);
        ListNode* node = head;
        while (node->next && node->next->val < node->val) {
            swap(node->val, node->next->val);
            node = node->next;
        return head;

Log in to reply

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