445-Add Two Numbers II

[难度] medium
[分类] linked list

1.题目描述

You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

2.测试样例

Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 8 -> 0 -> 7

3.算法分析

输入是两个链表,要将链表中的数字从右向左对应相加,最终得到结果链表。
(1)将输入的两个链表进行反转,采用栈实现,先将链表中的结点从左到右push到栈中,然后将栈中的节点pop即可得到相应的反转链表。
(2)将反转后的链表对应数字进行相加,注意进位的处理。
(3)得到的链表再次利用栈进行反转即可得到最终结果。

4.代码实现

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        stack<ListNode*> mystack;
        //  get reversed list l1
        ListNode* p = l1;
        while (p != NULL) {
            mystack.push(p);
            p = p->next;
        }
        if (!mystack.empty()) {
            p = mystack.top();
            mystack.pop();
            l1 = p;
        }
        while (!mystack.empty()) {
            p->next = mystack.top();
            mystack.pop();
            p = p->next;
        }
        if (p != NULL)
            p->next = NULL;
        //  get reversed list l2
        ListNode* p2 = l2;
        while (p2 != NULL) {
            mystack.push(p2);
            p2 = p2->next;
        }
        if (!mystack.empty()) {
            p2 = mystack.top();
            mystack.pop();
            l2 = p2;
        }
        while (!mystack.empty()) {
            p2->next = mystack.top();
            mystack.pop();
            p2 = p2->next;
        }
        if (p2 != NULL)
            p2->next = NULL;
        //  sum correspond num, notice the carry
        int carry = 0;
        ListNode* head = NULL;
        if (l1 != NULL && l2 != NULL) {
            carry = (l1->val + l2->val) / 10;
            head = new ListNode((l1->val + l2->val)%10);
            l1 = l1->next;
            l2 = l2->next;
        }
        else if (l1 != NULL && l2 == NULL) {
            head = new ListNode(l1->val);
            l1 = l1->next;
        }
        else if (l1 == NULL && l2 != NULL) {
            head = new ListNode(l2->val);
            l2 = l2->next;
        }
        ListNode* result = head;
        int sum = 0;
        while (l1 != NULL || l2 != NULL) {
            if (l1 != NULL && l2 != NULL) {
                if (carry == 1) sum = l1->val + l2->val + 1;
                else sum = l1->val + l2->val;
                carry = sum / 10;
                head->next = new ListNode(sum % 10);
                l1 = l1->next;
                l2 = l2->next;
            }
            else if (l1 != NULL && l2 == NULL) {
                head->next = new ListNode((l1->val + carry)%10);
                carry = (l1->val + carry) / 10;
                l1 = l1->next;
            }
            else if (l1 == NULL && l2 != NULL) {
                head->next = new ListNode((l2->val + carry)%10);
                carry = (l2->val + carry) / 10;
                l2 = l2->next;
            }
            head = head->next;
        }
        //  if the final carry is one
        if (carry == 1) {
            head->next = new ListNode(1);
            head = head->next;
        }
        head->next = NULL;
        //  reverse the final list and get result
        while (result != NULL) {
            mystack.push(result);
            result = result->next;
        }
        if (!mystack.empty()) {
            result = mystack.top();
            mystack.pop();
            head = result;
        }
        while (!mystack.empty()) {
            head->next = mystack.top();
            mystack.pop();
            head = head->next;
        }
        head->next = NULL;
        return result;
    }
};

main函数(用于测试):

void print(ListNode* l1) {
    cout << l1->val;
    l1 = l1->next;
    int count = 1;
    while (l1 != NULL) {
        cout << " -> " << l1->val;
        l1 = l1->next;
        count++;
    }
    cout << "   length: " << count << endl;
}
int main() {
    int n1, n2;
    cout << "len1 and len2:";
    cin >> n1 >> n2;
    int num;
    ListNode* head = NULL;
    ListNode* l1 = NULL;
    ListNode* l2 = NULL;
    int i = n1, j = n2;
    while (i--) {
        cin >> num;
        if (i == (n1 -1)) {
            head = new ListNode(num);
            l1 = head;
        }
        else {
            head->next = new ListNode(num);
            head = head->next;
        }
    }
    while (j--) {
        cin >> num;
        if (j == (n2-1)) {
            head = new ListNode(num);
            l2 = head;
        }
        else {
            head->next = new ListNode(num);
            head = head->next;
        }
    }
    print(l1);
    print(l2);
    ListNode* temp = addTwoNumbers(l1, l2);
    if (temp == NULL) {
        cout << "here" << endl;
        system("pause");
        return 0;
    }
    print(temp);

    system("pause");
    return 0;
}

5.小结

在处理链表的时候,要特别注意对head结点单独进行处理,还有将最后结点的next设置为NULL,链表的处理比较容易出错,要特别注意链表为空的情况并对其进行相应的处理。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值