445-Add Two Numbers II

来源:互联网 发布:妇产科网络咨询成本 编辑:程序博客网 时间:2024/05/19 14:51

[难度] 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,链表的处理比较容易出错,要特别注意链表为空的情况并对其进行相应的处理。

原创粉丝点击