leetcode-algorithms-2 Add Two Numbers
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-2 Add Two Numbers的更多相关文章
- leetcode 第二题Add Two Numbers java
链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...
- 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...
- 《LeetBook》LeetCode题解(2):Add Two Numbers [M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- C# 写 LeetCode Medium #2 Add Two Numbers
2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...
- LeetCode 第二题 Add Two Numbers 大整数加法 高精度加法 链表
题意 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...
- leetcode@ [2/43] Add Two Numbers / Multiply Strings(大整数运算)
https://leetcode.com/problems/multiply-strings/ Given two numbers represented as strings, return mul ...
- 【一天一道leetcode】 #2 Add Two Numbers
一天一道leetcode系列 (一)题目: You are given two linked lists representing two non-negative numbers. The digi ...
- leetcode题解2. Add Two Numbers
题目: You are given two non-empty linked lists representing two non-negative integers. The digits are ...
- 【LeetCode练习题】Add Two Numbers
链表相加 You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- LeetCode OJ 2. Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
随机推荐
- [JavaScript] - replaceAll,将字符串中的字母或数字等全部替换掉的方式
原题 function DNAtoRNA(dna) { // create a function which returns an RNA sequence from the given DNA se ...
- HBase与列存储
传统的行存储和(HBase)列存储的区别 1.为什么要按列存储 列式存储(Columnar or column-based)是相对于传统关系型数据库的行式存储(Row-basedstorage)来说的 ...
- Python中的垃圾回收机制
Python的垃圾回收机制 引子: 我们定义变量会申请内存空间来存放变量的值,而内存的容量是有限的,当一个变量值没有用了(简称垃圾)就应该将其占用的内存给回收掉,而变量名是访问到变量值的唯一方式,所以 ...
- 8、nginx和tengine简介
练习: 使用nginx反向代理(rr调度)用户请求至两个以上的后端LAMP(按标准路径部署的有pma,wd),不管用户请求是什么内容都反向代理至后端服务器去,但是如果用户请求的是图片或者是html,就 ...
- Python 基础 Python是什么
1.Python 是一门高级的.面向对象的,解释性,脚本语言.
- super()、this属性与static静态方法的执行逻辑
1.super的构造顺序:永远优先构造父类的方法 2.static永远在类实例之前执行,this的使用范围为实例之后
- 【selenium2】【unittest】
#栗子 from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver ...
- _itemmod_extra_equipments_enable
如果这个表是空的 所有装备都可以当成双甲 如果这个表里有配置物品 那么只有表里的 才可以当成双甲 表说明 `comment` 备注 `entry` 物品ID
- 《EM-PLANT仿真技术教程》读书笔记
1.在系统分析过程中,必须考虑系统所处的环境,因此划分系统与环境的边界是系统分析的首要任务 2.模型可以分为物理模型和数学模型.数学模型可以分为解析模型.逻辑模型.网络模型以及仿真模型.模型可以分为离 ...
- Android 错误集合
1. Error:java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 erro ...