【LeetCode】002 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.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
题解:
/**
* 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 dummy(-);
ListNode* pre = &dummy;
int carry = ; while (l1 || l2) {
int addend1 = l1 == nullptr ? : l1->val;
int addend2 = l2 == nullptr ? : l2->val;
int sum = addend1 + addend2 + carry;
ListNode* cur = new ListNode(sum % );
pre->next = cur;
pre = pre->next;
carry = sum / ; if (l1)
l1 = l1->next;
if (l2)
l2 = l2->next;
} if (carry) {
ListNode* cur = new ListNode(carry);
pre->next = cur;
}
return dummy.next;
}
};
【LeetCode】002 Add Two Numbers的更多相关文章
- 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...
- 【Leetcode】445. Add Two Numbers II
You are given two non-empty linked lists representing two non-negative integers. The most significan ...
- 【LeetCode】2.Add Two Numbers 链表数相加
题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...
- 【LeetCode】2.Add Two Numbers
首先想到的是走到其中一个链表的尽头,然后把剩余的链表中的值放入写的链表,返回,但是自己写的代码好长. struct ListNode* addTwoNumbers(struct ListNode* l ...
- 【LeetCode】2. Add Two Numbers 两数相加
给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和 ...
- 【LeetCode】165. Compare Version Numbers 解题报告(Python)
[LeetCode]165. Compare Version Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】623. Add One Row to Tree 解题报告(Python)
[LeetCode]623. Add One Row to Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problem ...
- 【LeetCode】989. Add to Array-Form of Integer 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数组转整数再转数组 模拟加法 日期 题目地址:htt ...
- 【一天一道leetcode】 #2 Add Two Numbers
一天一道leetcode系列 (一)题目: You are given two linked lists representing two non-negative numbers. The digi ...
随机推荐
- imx6qsbd lvds dtc
lvds显示屏调试参考 1.基于飞思卡尔imxsolosabresd开发板Linux-3.10.53 lvds屏幕调试: http://blog.csdn.net/qq_37375427/articl ...
- 常用模块----time&random&hushlib&os
模块:本质就是一个.py文件 分为三部分: 内置模块 第三方模块 自定义模块(模块调用,包) 加载顺序:内置模块——>自定义模块 time 模块 # <1> 时间戳 >&g ...
- Python编程-网络编程
一.Socket复习 1.Socket参数 sk.bind(address) 必会 s.bind(address) 将套接字绑定到地址.address地址的格式取决于地址族.在AF_INET下,以元组 ...
- Windows下MarialDB使用
命令行控制启动和关闭:mysqld --console #这样启动ctrl+c即为关闭 启动:双击mysqld.exe即可 #此为后台启动 关闭:mysqladmin -uroot -pr ...
- Vue.js学习笔记 第八篇 组件
全局注册组件 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <ti ...
- RDLC 微软报表 自定义函数
报表的空白处点右键,报表属性,CODE,按下面的格式输入自定义函数: Shared Function ShowDate(value as DateTime) as string if value< ...
- MongoDB快速入门(一)
MongoDB是一个跨平台,面向文档的数据库,提供高性能,高可用性和易于扩展.MongoDB是工作在集合和文档上一种概念. 数据数 数据库是一个集合的物理容器.每个数据库获取其自己设定在文件系统上的文 ...
- mybatis 一对多和多对一
在学习MyBatis3的过程中,文档上面一直在强调一个id的东西!在做这个实验的时候,也因为没有理解清楚id含义而导致一对多的“多”中也只有一条数据.id和result的唯一不同是id表示的结果将 ...
- 网络最大流的(Edmond Karp)算法
容量网络:在有向图D=(V,A),指定一个点为发点,记作 s,指定另一个点为收点,记作 t,其余点叫作中间点.对于A的每条弧(Vi,Ai),都对应一个权数 C ≥0,称为弧(Vi , Ai)的容量,将 ...
- Elasticsearch-->Get Started-->Basic concepts
https://www.elastic.co/guide/en/elasticsearch/reference/current/getting-started-concepts.html There ...