【题解】【链表】【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
思路:乍一看以为是大整数,实则相当简单,比Add Binary还要简单
代码:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
return addTwoDigit(l1,l2,);
}
ListNode *addTwoDigit(ListNode *d1, ListNode *d2, int carry){
int sum = carry;
ListNode *n1 = d1, *n2 = d2;//不要轻易修改输入参数, 尤其是链表问题中
if(d1 != NULL){
sum += d1->val;
n1 = d1->next;
}
if(d2 != NULL){
sum += d2->val;
n2 = d2->next;
}
ListNode *newNode = new ListNode(sum%); if(d1 == NULL && d2 == NULL){
if(sum == )
return NULL;//Avoid test case:{0}, {0} => {0,0}
return newNode;
} ListNode *nextNode = addTwoDigit(n1, n2, sum/);
newNode->next = nextNode;
return newNode;
}
【题解】【链表】【Leetcode】Add Two Numbers的更多相关文章
- 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 Numbers: You are given two non-empty linked lists representing two non-negative integers. Th ...
- [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分析和实现
Add Two Numbers这个问题的意思是,提供两条链表,每条链表表示一个十进制整数,其每一位对应链表的一个结点.比如345表示为链表5->4->3.而我们需要做的就是将两条链表代表的 ...
- LeetCode: Add Two Numbers 解题报告
Add Two NumbersYou are given two linked lists representing two non-negative numbers. The digits are ...
- [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
题目链接 题目要求: You are given two linked lists representing two non-negative numbers. The digits are stor ...
- 【链表】Add Two Numbers
题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...
- [Leetcode] Add two numbers 两数之和
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
随机推荐
- [转]Java并发的四种风味:Thread、Executor、ForkJoin和Actor
这篇文章讨论了Java应用中并行处理的多种方法.从自己管理Java线程,到各种更好几的解决方法,Executor服务.ForkJoin 框架以及计算中的Actor模型. Java并发编程的4种风格:T ...
- android 定制目录
首先简单介绍一下安卓系统文件夹对照表 主要介绍的是Android系统的文件夹结构,帮助大家更直观地了解系统 \\system\\app这个里面主要存放的是常规下载的应用程序,可以看到都是以APK格式结 ...
- [转]BeginInvoke和EndInvoke方法浅析
开发语言:C#3.0 IDE:Visual Studio 2008 一.C#线程概述 在操作系统中一个进程至少要包含一个线程,然后,在某些时候需要在同一个进程中同时执行多项任务,或是为了提 ...
- 扫盲如何在ECLIPSE中使用条件断点
有时候在编码的时候我们希望知道代码变量符合某个条件时,才中断点,其他的情况不中断点. 解决办法1: 我们写个代码 判断,符合条件在符合条件处进行断点,这个方法很麻烦,需要去修改代码,不要是还需 ...
- ASP.net 验证码(C#) MVC
ASP.net 验证码(C#) MVC http://blog.163.com/xu_shuhao/blog/static/5257748720101022697309/ 网站添加验证码,主要为防止机 ...
- xml数据解析调研
XML数据解析http://www.tuicool.com/articles/Nraau2(必用) http://www.cnblogs.com/pengyingh/articles/2342699. ...
- CNAPS Code 查询(招商银行)
招商银行的妹子实在太傻了,根本不知道什么是CNAPS Code.联行号,完全答非所问. 最后还是自己搞定了,如图: 最后再看看招行人员的英语水平,真是不知道什么是东西:
- php头像上传并裁剪支持3个尺寸
源码下载
- VS设置背景色减缓眼睛疲劳
工具--选项--字体和颜色--(纯文本)项背景色--自定义... 色调:85 饱和度:123 亮度:205 可自己微调 字体设为10.
- js和C#中的编码和解码
同一个字符串,用URL编码和HTML编码,结果是完全不同的. JS中的URL编码和解码.对 ASCII 字母和数字及以下特殊字符无效: - _ . ! ~ * ' ( ) ,/?:@&=+$# ...