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 ...
随机推荐
- 深度学习课程笔记(十三)深度强化学习 --- 策略梯度方法(Policy Gradient Methods)
深度学习课程笔记(十三)深度强化学习 --- 策略梯度方法(Policy Gradient Methods) 2018-07-17 16:50:12 Reference:https://www.you ...
- (转)ResNet, AlexNet, VGG, Inception: Understanding various architectures of Convolutional Networks
ResNet, AlexNet, VGG, Inception: Understanding various architectures of Convolutional Networks by KO ...
- (转载)Unity里实现更换游戏对象材质球
在unity中本来想实现在一个背景墙上更换图片的功能 在网上查了一些资料说是用Image,但我是新手小白刚接触Unity不久好多组建还不会用,就想能不能通过改变游戏对象的材质球来更换游戏对象的背景. ...
- Twitter开发2
There are different API families The standard (free) Twitter APIs consist of REST APIs and Streaming ...
- QT中QLineEdit的editingFinished()信号在按下回车时会触发两次的解决办法
class MyLineEdit : public QLineEdit { Q_OBJECT public: MyLineEdit(QWidget * parent = 0) : QLineEdit( ...
- QLineEdit响应回车时避免Button同时响应
pButton->setAutoDefault(false);
- SSH KEY 设置 目录在open ~ 根目录下的.ssh 里面
当我们从github或者gitlab上clone项目或者参与项目时,需要证明我们的身份.github.gitlab支持使用SSH协议进行免密登录,而SSH协议采用了RSA算法保证了登录的安全性.我们要 ...
- 理解 Redis(7) - Set 值
unordered collection of unique strings.set值是唯一的字符串的无序集合, 把握住两个特点: 唯一, 无序. 清空所有的数据, 并清理显示界面: 127.0.0. ...
- spring boot 当参数传入开头多个0时,报错:JSON parse error: Invalid numeric value: Leading zeroes not allowed
原因是: Jackson解析json配置的问题 在配置文件中设置下: spring.jackson.parser.allow-numeric-leading-zeros=true
- Windows下及Mac下的IntelliJ IDEA快捷键
Mac 键盘符号说明 ⌘ == Command ⇧ == Shift ⇪ == Caps Lock ⌥ == Option ⌃ == Control ↩ == Return/Enter ⌫ == De ...