题目:

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){}
};

leetcode题解2. Add Two Numbers的更多相关文章

  1. 《LeetBook》LeetCode题解(2):Add Two Numbers [M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  2. LeetCode题解 #2 Add Two Numbers

    题目大意:使用链表表示的两个整数,计算出其和,以同样的形式返回. Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 ...

  3. LeetCode 题解之Add Two Numbers II

    1.题目描述 2.分析 首先将链表翻转,然后做加法. 最后将结果链表翻转. 3.代码 ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { Lis ...

  4. LeetCode题解之Add two numbers

    1.题目描述 2.题目描述 题目思路可以参考合并单链表的思路,定义一个全局 进位标志,如果两个数值相加得到需要进位,则将进位标志置为1 . 3.代码 ListNode* addTwoNumbers(L ...

  5. leetcode 第二题Add Two Numbers java

    链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...

  6. 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...

  7. C# 写 LeetCode Medium #2 Add Two Numbers

    2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...

  8. LeetCode 第二题 Add Two Numbers 大整数加法 高精度加法 链表

    题意 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...

  9. leetcode@ [2/43] Add Two Numbers / Multiply Strings(大整数运算)

    https://leetcode.com/problems/multiply-strings/ Given two numbers represented as strings, return mul ...

随机推荐

  1. Quartz.net定时任务框架的使用

    一:Nuget添加Quartz.net和Topshelf 二:新建HelloJob类继承IJob public class HelloJob : IJob       {            pub ...

  2. 怎样从外网访问内网Memcached数据库

    外网访问内网Memcached数据库 本地安装了Memcached数据库,只能在局域网内访问,怎样从外网也能访问本地Memcached数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装 ...

  3. Python 匿名变量

    匿名变量的使用 calc = lambda x:x*3 print(calc(3)) 注:匿名变量是没有名字的变量 注:使用:lambda 创建.

  4. CMD控制器常用命令

    dir 查看当前路径文件cd..返回上一级路径cd 转到指定的文件夹 \n 将光标移动到下一行的第一格 \t 将光标移动到下一个水平制表位置 mspaint 画图 编译源代码 javac HelloW ...

  5. [转载]Black-Scholes 模型中 d1,d2 是怎么得到的?如何理解 Black-Scholes 模型?

    https://www.optbbs.com/thread-253244-1-1.html

  6. Linux md5sum 的用法

    MD5 算法常常被用来验证网络文件传输的完整性,防止文件被篡改.MD5 全称是报文摘要算法,此算法对任意长度 的信息逐位计算,产生一个二进制长度为 128 位(十六进制长度 32 位)的报文摘要,不同 ...

  7. C# Array 基本数据类型数组的基类 传参问题

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  8. ONOS架构-系统组件

    系统组件 系统分层 App/core/providers 业务和子系统 一个业务service是有多个组件构成的功能单元,基于各层软件栈提供一个垂直的分片slice,将构成业务service的组件集合 ...

  9. three.js 创建点 线 面

    <html> <head> <title>My first three.js app</title> <style> body { marg ...

  10. springboot应用无故停止运行killed解决方法

    最近使用springboot开发了一个ip代理的程序,今天放到阿里云服务器上运行,多次出现应用运行突然停止的问题. 使用free -h 查看内存使用完全正常.重新运行监视CPU使用也正常.没有出现堆内 ...