[LeetCode] Add Two Numbers
You are given two linked lists representing two non-negative numbers. 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.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
这题虽然归类到Medium 但是难度不大,只是要考虑的东西有点多,比如:两个list不一定一样长,进位这两个问题。
因为两个数字是逆序存到链表里的,所以这大大简化了问题难度,因为只需要向着链表遍历方向往前进位就可以了。
void addIter(ListNode *l1, ListNode *l2, ListNode *prev, int carry) {
if (!l1 && !l2 && carry == ) return;
ListNode *node;
int val = ;
if (l1) {
val += l1->val;
}
if (l2) {
val += l2->val;
}
val += carry;
carry = val / ;
val = val % ;
node = new ListNode(val);
if (prev) {
prev->next = node;
}
addIter(l1 ? l1->next : NULL, l2 ? l2->next : NULL, node, carry);
}
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
ListNode *ret = new ListNode();
addIter(l1, l2, ret, );
return ret->next;
}
[LeetCode] Add Two Numbers的更多相关文章
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [LeetCode] Add Two Numbers 两个数字相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- LeetCode Add Two Numbers II
原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...
- LeetCode: Add Two Numbers 解题报告
Add Two NumbersYou are given two linked lists representing two non-negative numbers. The digits are ...
- Leetcode:Add Two Numbers分析和实现
Add Two Numbers这个问题的意思是,提供两条链表,每条链表表示一个十进制整数,其每一位对应链表的一个结点.比如345表示为链表5->4->3.而我们需要做的就是将两条链表代表的 ...
- [LeetCode] Add Two Numbers题解
Add Two Numbers: You are given two non-empty linked lists representing two non-negative integers. Th ...
- [Leetcode] Add two numbers 两数之和
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- LeetCode——Add Two Numbers
Question:You are given two linked lists representing two non-negative numbers. The digits are stored ...
- [LeetCode] Add Two Numbers 链表
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
随机推荐
- net-snmp5.7.3移植到arm-linux平台
net-snmp5.7.3移植到arm-linux平台 本次交叉编译在ubuntu 15.04 64位系统下进行. 准备工作 在编译移植前有几项准备工作需要完成. 1下载net-snmp 5.7.3源 ...
- PHP5.4开启zend opcache缓存
PHP5.5整合了zend opcache,但是5.4以下的需要自己安装. 介绍一下开启opcache方法,环境为wamp(php5.4.16) 下载dll文件 把php_opcache.dll放进p ...
- [mysql]max_allowed_packet ,centos
在通过脚本向mysql写入大量测试数据时,出现这个问题,记录下: https://dev.mysql.com/doc/refman/5.5/en/packet-too-large.html 修改/et ...
- 使用SharePoint 2010 母版页
SharePoint 2010母版页所用的还是ASP.NET 2.0中的技术.通过该功能,实现了页面框架布局与实际内容的分离.虽然在本质上自定义母版页的过程和以前版本的SharePoint大致相同,但 ...
- MySQL Plugin 'InnoDB' init function returned error
. . 在MySQL的配置文件中,设定default-table-type=InnoDB,发现MySQL无法正常的启动,错误日志中给出了如下的信息: 150210 18:11:19 mysqld_sa ...
- vc++创建文件目录
#include "stdafx.h" #include <iostream> #include <fstream> #include <string ...
- markdown 基本语法
代码块: ```console.log(1);```--- 标题: # h1## h2### h3 --- 粗斜体: *斜体***粗体*****粗斜体*** --- 强调:`强调` --- 链接:[百 ...
- UrlEncoder url编码
public static string PercentEncode(string s) { var bytes = Encoding.UTF8.GetBytes( ...
- Django~queries
API queries create, retrieve, update and delete
- 【leetcode】Gray Code (middle)
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...