My easy C solution.


  • 0
    P
    struct ListNode* insertionSortList(struct ListNode* head) {
        if (head == NULL || head->next == NULL)
            return head;
        
        struct ListNode **p, *node, *next;
        
        node = head->next;
        head->next = NULL;
        while (node) {
            p = &head;
            next = node->next;
            while (*p && (*p)->val <= node->val) {
                p = &(*p)->next;
            }
            if (*p) {
                node->next = *p;
                *p = node;
            } else {
                node->next = NULL;
                *p = 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.