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. log4j2打印Mybatis执行的SQL语句及SQL语句的执行时间

    http://blog.csdn.net/zjq852533445/article/details/78320012

  2. Jquery loading 效果

    function showLoad(tipInfo) { var eTip = document.createElement('div'); eTip.setAttribute('id', 'tipD ...

  3. 单域名下多子域名同时认证HTTPS

    参考: http://blog.csdn.net/wzj0808/article/details/53401101 http://www.cnblogs.com/silin6/p/5931640.ht ...

  4. ngui项目花屏问题

    项目用的ngui 最近在金立手机上遇到一个问题就是  启动的时候会花一下屏幕 一闪而过  由于ngui默认camera使用的是clear depth  所以按照网上的办法修改 color 跟skybo ...

  5. vue项目webpack打包后有的文件big 问题

    vue项目打包的时候,有的big, 超过1M,   需要进行优化,方法有: 1.  非首屏图片,可以采用懒加载的方式,  如:图片的懒加载,  vue中路由的懒加载 等 2. 各个模块, 采用如sea ...

  6. 力扣(LeetCode)292. Nim游戏 巴什博奕

    你和你的朋友,两个人一起玩 Nim游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头. 拿掉最后一块石头的人就是获胜者.你作为先手. 你们是聪明人,每一步都是最优解. 编写一个函数,来判断你 ...

  7. vue-cli中理不清的assetsSubDirectory 和 assetsPublicPath

    背景 一般情况下,我们借用 vue-cli之力安装好所有依赖后,我们就可以愉快的板砖了.但是也经常会遇到一写问题,比如assetsSubDirectory 和 assetsPublicPath两个兄弟 ...

  8. 学习笔记32—python常见问题及解决办法

    1.Anaconda3 中 Spyder 无法打开/点击没有反应 应对方法 1).通过pip安装pyqt5:pip install pyqt5 2).输入以下命令:spyder --new-insta ...

  9. C++中的清屏函数

    system("cls") 执行控制台命令cls,功能是清屏,清楚所有屏幕显示信息

  10. JS添加/移除事件

    事件的传播方式 <div id="father"> <div id="son"></div> </div> &l ...