Double star programming


  • 0
    P

    Not a good idea to use double star here. Easy to make mistakes.

    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {   // all splice to l2
        if(!l1) return l2;
        if(!l2) return l1;
        ListNode **p2 = &l2;
        while(l1 && *p2) {
            if((*p2)->val <= l1->val) {
                p2 = &((*p2)->next);
            } else {
                ListNode* nd = l1;
                l1 = l1->next;
                nd->next = *p2;
                (*p2) = nd;
                p2 = &(nd->next);
            }
        }
        if(l1) (*p2) = l1;
        return l2;
    }

Log in to reply
 

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