leetcode-algorithms-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.

解法

同时遍历两个链表,对值进行相加.得到的值%10就是新的链表的值,同时存下/10(只可能是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 *l = nullptr;
ListNode *lt = nullptr;
ListNode *node1 = l1;
ListNode *node2 = l2;
int lastaddval = 0;
while(true)
{
if (node1 == nullptr && node2 == nullptr) break; int nodeval1 = 0;
int nodeval2 = 0;
if (node1 != nullptr)
{
nodeval1 = node1->val;
node1 = node1->next;
}
if (node2 != nullptr)
{
nodeval2 = node2->val;
node2 = node2->next;
}
int sumval = nodeval1 + nodeval2 + lastaddval;
lastaddval = sumval / 10;
if (l == nullptr)
{
l = new ListNode(sumval % 10);
lt = l;
}
else
{
ListNode *n = new ListNode(sumval % 10);
lt->next = n;
lt = n;
}
}
if (lastaddval != 0)
{
ListNode *n = new ListNode(lastaddval);
lt->next = n;
lt = n;
}
return l;
}
};

时间复杂度: O(max(m,n)).m和n分别是l1和l2的长度.

空间复杂度: O(max(m,n)).

链接: leetcode-algorithms 目录

leetcode-algorithms-2 Add Two Numbers的更多相关文章

  1. leetcode 第二题Add Two Numbers java

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

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

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

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

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

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

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

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

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

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

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

  7. 【一天一道leetcode】 #2 Add Two Numbers

    一天一道leetcode系列 (一)题目: You are given two linked lists representing two non-negative numbers. The digi ...

  8. leetcode题解2. Add Two Numbers

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

  9. 【LeetCode练习题】Add Two Numbers

    链表相加 You are given two linked lists representing two non-negative numbers. The digits are stored in ...

  10. LeetCode OJ 2. Add Two Numbers

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

随机推荐

  1. Nuget 打包 for .Net Standart project

    Create .NET Standard packages with Visual Studio 2015 Publishing packages nuge.exe 放在项目目录中 nuget spe ...

  2. 17秋 SDN课程 第五次上机作业

    17秋 SDN课程 第五次上机作业 Project:https://github.com/Wasdns/new_balance Slide is available at https://github ...

  3. [0413] FFTSHIFT的四种写法

    FFTSHIFT的四种写法 前言 matlab说,"你读过书,--我便考你一考.fftshift的函数,怎样写的?"我想,讨饭一样的人,也配考我么?便回过脸去,不再理会.matla ...

  4. Jenkins参数化构建(一)之 Maven Command Line传递TestNG构建参数

    1. Maven使用 -D参数名称 将参数传递至所运行项目 Maven指定TestNg.xml文件 clean test -DsuiteXmlFile=src/main/resources/testn ...

  5. 【Python】【一些概念与对比】

    type.__new__()  : 返回类.可以把类看作是metaclass 创建出来的实例 普通类里的__new__() : 返回类的实例. __new__() : 返回类的实例.Python解释器 ...

  6. 恢复Intellij idea删除的文件

    恢复Intellij idea的删除文件方法: 右键单机项目名称---->Local History---->Show History 可以看到历史操作记录,右键单机想要恢复的文件---- ...

  7. eclipse中.project文件和.classpath文件详解

    一.概述.project是项目文件,项目的结构都在其中定义,比如lib的位置,src的位置,classes的位置..classpath的位置定义了你这个项目在编译时所使用的$CLASSPATH. 二. ...

  8. nginx的使用教程

    一.基本概念 1.1 正向代理和反向代理 (参考文档:https://www.cnblogs.com/hafiz/p/7233306.html) 假设我们给定客户端A.代理服务器B.以及最终服务器C ...

  9. [osg][opengl]透视投影的参数Perspective

    gluPerspective这个函数指定了观察的视景体(frustum为锥台的意思,通常译为视景体)在世界坐标系中的具体大小,一般而言,其中的参数aspect应该与窗口的宽高比大小相同.比如说,asp ...

  10. 学习笔记15—Python 存储集

    1. save using pickle      with open('F:/BrainAging/result/ordered_data_final_just_TD_leaveOne.pickle ...