LeetCode_2 add_two_numbers 两数之和

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sumword_/article/details/86773432

简单题

ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
    ListNode * l3, *head;
    head = new ListNode(-1);
    l3 = head;
    int flag=0;
    while(l1 || l2 || flag){
        int sum = 0;
        if(l1)
            sum += l1->val;
        if(l2)
            sum += l2->val;
        sum+=flag;
        l3->next = new ListNode(sum%10);
        l3 = l3->next;
        flag = sum/10;
        if(l1)l1 = l1->next;
        if(l2)l2 = l2->next;
    }
    return head->next;
}

猜你喜欢

转载自blog.csdn.net/sumword_/article/details/86773432