Leetcode题解 - 445. Add Two Numbers II

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.
Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.

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

链接

正解

双栈法,由于题目要求不能直接将两个链表反转然后在相加,因此采用两个栈存储链表中的元素,再诸位相加插入到新的链表中即可,插入时由于该题目链表采用头指针的方式,对应进行相应的处理。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        vector<int> v1,v2;
        ListNode *p=l1,*q=l2;
        for(;p!=NULL;p=p->next){
            v1.push_back(p->val);
        }
        for(;q!=NULL;q=q->next){
            v2.push_back(q->val);
        }
        int size1=v1.size(),size2=v2.size();
        int sum=0,carry=0;
        ListNode *head=nullptr;
        for(int i=size1-1,j=size2-1;i>=0||j>=0||carry>0;i--,j--){
            sum=carry;
            if(i>=0) sum+=v1[i];
            if(j>=0) sum+=v2[j];
            carry=sum/10;
            ListNode* tmp=new ListNode(sum%10);
            tmp->next=head;
            head=tmp;
        }
        return head;
    }
};

某错解

先遍历链表得到链表的长度,按高位到低位依次将数字存储在一个long int中,再直接相加,最后按位取数转存到新的链表之中,因为long型存储的数字也有位数的限制,会导致溢出,其中具体的例子为

Input:
[2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,9]
[5,6,4,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,9,9,9,9]
Output:
[-9,-1,-3,-7,0,-5,-8,-7,-2,-4,-2,-1,-2,-5,-8,-7,-5,-8,-9]
Expected:
[8,0,7,4,8,6,4,8,6,4,8,6,4,8,6,4,8,6,4,8,6,4,8,6,4,8,6,4,8,6,4,8,6,4,8,6,4,8,6,4,8,6,4,8,6,4,8,6,4,8,6,4,8,6,4,8,7,2,4,3,8]

代码如下

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode* p=l1;
        ListNode* q=l2;
        long size1=1,size2=1;
        long lnum=0,rnum=0;
        for(;p->next!=NULL;p=p->next){
            size1=size1*10;
        }
        for(;q->next!=NULL;q=q->next){
            size2=size2*10;
        }
        for(p=l1;p!=NULL;p=p->next){
            lnum+=p->val*size1;
            size1/=10;
        }
        for(q=l2;q!=NULL;q=q->next){
            rnum+=q->val*size2;
            size2/=10;
        }
        long result=lnum+rnum;
        if(result==0){
            return new ListNode(0);
        }
        ListNode* head=nullptr;
        while(result!=0){
            ListNode *tmp=new ListNode(result%10);
            tmp->next=head;
            head=tmp;
            result/=10;
        }
        return head;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值