My solution in C


  • 2
    D
    struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
        struct ListNode* res, *L1=l1, *L2=l2, *temp;
        while (L1&&L2) {
            L1->val=L2->val=L1->val+L2->val;
            L1 = L1->next;
            L2 = L2->next;
        }
        res = (L1) ? l1 : l2;
        temp=res;
        while (temp) {
            if (temp->val>=10) {
                if (!temp->next) temp->next = (struct ListNode*)calloc(1,sizeof(struct ListNode));
                temp->val-=10;
                temp->next->val+=1;
            } 
            temp = temp->next;
        }
        return res;
    

    }


  • 1
    B

    @DarkHitter If change the line "if (temp->val >=10) {" to "if (temp->val > 9) {" would save some times.


Log in to reply
 

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