C clear solution, 3 ms


  • 0
    J
    struct ListNode *p_master, *p_slave, *p_head, *p_temp;
        if (l1 == NULL && l2 == NULL) {
            return NULL;
        } else if (l1 == NULL) {
            return l2;
        } else if (l2 == NULL) {
            return l1;
        }
        if (l1->val <= l2->val) {
            p_head = p_master = l1;
            p_slave = l2;
        } else {
            p_head = p_master = l2;
            p_slave = l1;
        }
    
        while(p_slave) {
            if (p_master->next == NULL) {
                p_master->next = p_slave;
                break;
            }
            
            if (p_slave->val == p_master->val) {
                // insert to master
                p_temp = p_slave->next;
                p_slave->next = p_master->next;
                p_master->next = p_slave;
                p_slave = p_temp;
    
            } else if (p_master->next->val > p_slave->val) {
                // insert to master
                p_temp = p_slave->next;
                p_slave->next = p_master->next;
                p_master->next = p_slave;
                p_slave = p_temp;
    
            } else {
                p_master = p_master->next;
            }
        }
        return p_head;
    

Log in to reply
 

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