Leetcode题解

来源:互联网 发布:哈登15 16赛季数据 编辑:程序博客网 时间:2024/04/26 10:26

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;    }};
0 0