LeetCode 2. Add Two Numbers

LeetCode 2. Add Two Numbers


题目描述:

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order 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.

Example:

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

题目理解:

给定两个非空的链表,代表两个非负的整数,按照整数的倒序存储,每个结点存储一个数字,将两个链表相加,返回和

刚开始想把数从两个链表中读出来,然后加好后再存在新的链表中,但是有的用例的范围已经超过了longlongint ,所以这种方法不可取。

于是想法就是把每个结点相加,然后只要注意下长度和进位即可,不过这个我这个脑袋瓜思考只能单线程思考,写了满满一篇子代码,用了n多个分支,结果其实只是一个循环就能达到目的,也是醉了。。。

我的:

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode* ans = new ListNode(0);
        ListNode* head = ans;
        bool flag = false;
        while(l1!=NULL && l2!=NULL){
            ListNode* tmp; 
            int sum = l1->val + l2->val;
            if(flag){
                tmp = new ListNode((sum+1)%10);
            }else{
                tmp = new ListNode(sum%10);
            }
            ans -> next = tmp;
            ans = tmp;
            if(flag){
                if((sum+1)>=10){
                flag = true;
                }else{
                    flag = false;
                }
            }else{
                if(sum >=10){flag = true;}
                else{flag = false;}
            }
            l1 = l1->next;
            l2 = l2->next;
        }
        if(l1==NULL && l2!=NULL){
            ans->next = l2;
            if(flag){
                while(l2!=NULL&&flag){
                    int t = l2->val + 1;
                    l2->val = t%10;
                    if(t>=10) flag = true;
                    else flag = false;
                    ans = l2;
                    l2 = l2->next;
                }
                if(flag){
                    ListNode* tmp = new ListNode(1);
                    ans->next = tmp;
                    flag = false;
                }
            }
        }
        if(l1!=NULL && l2==NULL){
            ans->next = l1;
            if(flag){
                while(l1!=NULL&&flag){
                    int t = l1->val + 1;
                    l1->val = t%10;
                    if(t>=10) flag = true;
                    else flag = false;
                    ans = l1;
                    l1 = l1->next;
                }
                if(flag){
                    ListNode* tmp = new ListNode(1);
                    ans -> next = tmp;
                    flag = false;
                }
            }
        }
        if(flag){
            ListNode* tmp = new ListNode(1);
            ans->next = tmp;
            ans = tmp;
        }
        return head->next;
    }
};

别人的:

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        int sum = 0;
        ListNode* h = new ListNode(-1);
        ListNode *t = h;
        while (l1 || l2 || sum) {
            if (l1) {//这样分支,不久解决长度问题了。。。。
                sum += l1->val;
                l1 = l1->next;
            }
            if (l2) {//也不用担心加不加,有就加,没有就不加呗。。。
                sum += l2->val;
                l2 = l2->next;
            }
            t->next = new ListNode(sum % 10);
            t = t->next;
            sum /= 10;//这不就解决进位问题了!我真的是傻。。。
        }
        return h->next;
    }
};

虽然思路将自己蠢哭,不过还是很好的练习了链表和动态内存分配,还可以把。。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值