Add Two Nums

Add Two Nums


*出错版本:
#include <iostream>
using namespace std;
struct ListNode {
      int val;
      ListNode *next;
      ListNode(int x) : val(x), next(NULL) {}
};
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2);
int main(void)
{
    ListNode *m = new ListNode(0);
    ListNode *n = new ListNode(0);
    ListNode *p = new ListNode(0);
    m->val = 0;
    m->next = NULL;
    n->val = 0;
    n->next = NULL;
    p = addTwoNumbers(m,n);
    while(p)
    {
        cout<<p->val<<endl;
    }   
    system("pause");
    return 0;
}
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode* a = l1;
        ListNode* b = l2;
        ListNode *c = new ListNode(0);
        ListNode *ans = c;
        ListNode *next = c;
        int car=0;
        while(a || b || car)
        {
            if((a = NULL) && (b = NULL) && (car !=0))//这里有重大错误,不能用=,而应该用==。后面同理。
            {
                c->val = car;
                c->next = NULL;
                car = 0;
            }
            else if(a = NULL)
            {
                c->val = b->val + car;
                b = b->next;
                ListNode *next = new ListNode(0);
                c->next = next;
                c = c->next;
                car = 0;
            }
            else if(b = NULL)
            {
                c->val = a->val + car;
                a = a->next;
                ListNode *next = new ListNode(0);
                c->next = next;
                c = c->next;
                car = 0;
            }
            else
            {
                c->val = ((a->val + b->val + car)%10);
                car = (a->val + b->val + car)/10;
                a = a->next;
                b = b->next;
                ListNode *next = new ListNode(0);
                c->next = next;
                c = c->next;
            }

        }
        return ans;
    }


* 调试过程:
#include <iostream>
//#include <stdio.h>
using namespace std;
struct ListNode {
      int val;
      ListNode *next;
      ListNode(int x) : val(x), next(NULL) {}
};
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2);
int main(void)
{
    ListNode *m = new ListNode(0);
    ListNode *n = new ListNode(0);
    ListNode *p = new ListNode(0);
    m->val = 5;
    m->next = NULL;
    n->val = 5;
    n->next = NULL;
    p = addTwoNumbers(m,n);
    while(p)
    {
        cout<<p->val<<endl;
        p=p->next;
    }
    getchar();
    return 0;
}
ListNode* addTwoNumbers(ListNode* a, ListNode* b) {
        //ListNode* a = new ListNode(0);
        //ListNode* b = new ListNode(0);
        ListNode *c = new ListNode(0);
        ListNode *ans = c;
        ListNode *pre = c;
        int car=0;
        /*
        while(a && b)
        {
            c = new ListNode(0);
            //c->val = ((a->val + b->val + car)%10);
            car = (a->val + b->val + car);
            c->val = car%10;
            car = car/10;
            a = a->next;
            b = b->next;
            pre->next = c;
            pre = pre->next;
        }
        while(a)
        {
            c = new ListNode(0);
            c->val = ((a->val + car)%10);
            car = (a->val + car)/10;
            a = a->next;
            pre->next = c;
            pre = pre->next;
        }
        while(b)
        {
            c = new ListNode(0);
            c->val = ((b->val + car)%10);
            car = (b->val + car)/10;;
            b = b->next;
            pre->next = c;
            pre = pre->next;
        }
        if(car)
        {
            c = new ListNode(0);
            c->val = car;
            c->next = NULL;
            pre->next = c;
            pre = pre->next;
            car = 0;
        }
        */


        while((a != NULL) || (b != NULL) || (car !=0))
        {
            if(a==NULL && b==NULL && (car !=0))
            {
                c = new ListNode(0);
                c->val = car;
                c->next = NULL;
                pre->next = c;
                pre = pre->next;
                car = 0;
            }
            else if(a==NULL)
            {
                c = new ListNode(0);
                c->val = ((b->val + car)%10);
                car = (b->val + car)/10;;
                b = b->next;
                pre->next = c;
                pre = pre->next;
            }
            else if(b==NULL)
            {
                c = new ListNode(0);
                c->val = ((a->val + car)%10);
                car = (a->val + car)/10;
                a = a->next;
                pre->next = c;
                pre = pre->next;
            }
            else
            {
                c = new ListNode(0);
                //c->val = ((a->val + b->val + car)%10);
                car = (a->val + b->val + car);
                c->val = car%10;
                car = car/10;
                a = a->next;
                b = b->next;
                pre->next = c;
                pre = pre->next;
            }

        }

        return ans->next;
    }   



* 正确版本:

#include <iostream>
//#include <stdio.h>这个和<iostream>只要一个就行
using namespace std;
struct ListNode {
      int val;
      ListNode *next;
      ListNode(int x) : val(x), next(NULL) {}
};
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2);
int main(void)
{
    ListNode *m = new ListNode(0);
    ListNode *n = new ListNode(0);
    ListNode *p = new ListNode(0);
    m->val = 0;
    m->next = NULL;
    n->val = 0;
    n->next = NULL;
    p = addTwoNumbers(m,n);
    while(p)
    {
        cout<<p->val<<endl;
        p=p->next;
    }
    getchar();
    return 0;
}
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
       int carry = 0;
        ListNode *list = new ListNode(0);
        ListNode *head = list;
        ListNode *prev = list;
        while (l1 && l2) {
            carry += l1->val + l2->val;

            list = new ListNode(0);
            list->val = carry % 10;
            carry /= 10;

            l1 = l1->next;
            l2 = l2->next;
            prev->next = list;
            prev = prev->next;
        }
        while (l1) {
            carry += l1->val;

            list = new ListNode(0);
            list->val = carry % 10;
            carry /= 10;

            l1 = l1->next;
            prev->next = list;
            prev = prev->next;
        }
        while (l2) {
            carry += l2->val;

            list = new ListNode(0);
            list->val = carry % 10;
            carry /= 10;

            l2 = l2->next;
            prev->next = list;
            prev = prev->next;
        }
        if (carry) {
            list = new ListNode(0);
            list->val = carry;
            prev->next = list;
            prev = prev->next;
        }
        return head->next;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值