leetcode题解2. Add Two Numbers

Posted on 2018-03-29 09:17  wangziyan  阅读(231)  评论(0)    收藏  举报

题目:

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.

首先先来分析一下题目
这个题目我的思路就是把这两个链表都转换成数字,然后相加,再将获得的和进行转化,转化为我们需要的链表结果。
看起来是个很完美的想法,不过很久没写代码还是发现了一些问题。
没有在IDE上写直接在leetcode上写报错了,
Line 20: request for member 'val' in 'l1', which is of pointer type 'ListNode*' (maybe you meant to use '->' ?报了这个错误,还是对结构体和指针的研究不够深入不知道什么时候用->什么时候用.
百度上搜到一个不错的答案:
->是指针指向其成员的运算符
.是结构体的成员运算符。
最大的区别是->前面放的是指针,而.前面跟的是结构体变量。
例如:
struct A
{
   int a;
   int b;
};
A *point = malloc(sizeof(struct A));
point->a = 1;
A object;
object.a = 1;
然后附上我这个思路的代码:
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        long long num1=0;
        long long num2=0;
        long long k=1;
        long long ans=0;
        ListNode* l3;
        ListNode* temp;
        while(true)
        {
            num1=(l1->val)*k+num1;
            k=k*10;
            if(l1->next==NULL) break;
            l1=l1->next;
        }
        k=1;
        while(true)
        {
            num2=l2->val*k+num2;
            k=k*10;
            if(l2->next==NULL) break;
            l2=l2->next;
        }
        ans=num1+num2;
        temp=new ListNode(0);
        l3=temp;
        while(true)
        {
            temp->val=ans%10;
            ans=ans/10;
            if(ans==0) break;
            temp->next=new ListNode(0);
            temp=temp->next;
        }
        return l3;
    }
};
这个方法并不能完全过数据,显然我忽略了一种样本数据很长的情况,从int换成long long也装不下这么多的数据。
所以需要换一个方法。
看了一下网上的和官方的解答思路,也就是需要将同一位的两个node里面的值加起来
当然也要考虑进位的问题,进位方面还有比较重要的就是如果最高位不够的情况要新增一个结点来存放最高位的1。
现在就展示一下后来的正确解法的代码部分:
/**
 * 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* l3=new ListNode(0);
        ListNode* temp=l3;
        int carry=0;
        while(l1!=NULL||l2!=NULL)
        {
            int n1=0;
            int n2=0;
            if(l1!=NULL) n1=l1->val;
            else n1=0;
            if(l2!=NULL) n2=l2->val;
            else n2=0;
            int n3=n1+n2+carry;  //sum is num1 and num2 and carry!
            carry=n3/10;
            n3=n3%10;
            if(temp!=NULL)
            {
                temp->next=new ListNode(n3);
            }
            temp=temp->next;
            if(l1!=NULL) l1=l1->next;
            if(l2!=NULL) l2=l2->next;
        }
        if(carry!=0)
        {
            temp->next=new ListNode(carry);
        }
        return l3->next;
    }
};
这道题也让我复习了一下链表,学会很好的使用链表是很必要的
学习建立一个简单的链表
struct ListNode{
  int val;
  ListNode *next;
  ListNode(int x) :val(x),next(NULL){}
};