[LintCode] Add Two Numbers 两个数字相加
You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1's digit is at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list.
Given 7->1->6 + 5->9->2. That is, 617 + 295.
Return 2->1->9. That is 912.
Given 3->1->5 and 5->9->2, return 8->0->8.
LeetCode上的原题,请参见我之前的博客Add Two Numbers。
class Solution {
public:
/**
* @param l1: the first list
* @param l2: the second list
* @return: the sum list of l1 and l2
*/
ListNode *addLists(ListNode *l1, ListNode *l2) {
ListNode *dummy = new ListNode(-), *cur = dummy;
int carry = ;
while (l1 || l2) {
int num1 = l1 ? l1->val : ;
int num2 = l2 ? l2->val : ;
int sum = num1 + num2 + carry;
cur->next = new ListNode(sum % );
carry = sum / ;
if (l1) l1 = l1->next;
if (l2) l2 = l2->next;
cur = cur->next;
}
if (carry) cur->next = new ListNode();
return dummy->next;
}
};
[LintCode] Add Two Numbers 两个数字相加的更多相关文章
- [LeetCode] Add Two Numbers 两个数字相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- [CareerCup] 2.5 Add Two Numbers 两个数字相加
2.5 You have two numbers represented by a linked list, where each node contains a single digit. The ...
- [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现
[LeetCode] Add Two Numbers 两个数字相加 You are given two non-empty linked lists representing two non-ne ...
- [LeetCode] 2. Add Two Numbers 两个数字相加
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- LeetCode-2. Add Two Numbers(链表实现数字相加)
1.题目描述 You are given two non-empty linked lists representing two non-negative integers. The digits a ...
- LeetCode 2. Add Two Numbers (两数相加)
题目标签:Linked List, Math 题目给了我们两个 Linked List, 各代表一个数字,不过顺序的反的.让我们把两个数字相加. 和普通的相加其实差不多,只不过变成了 Linked L ...
- [CareerCup] 18.1 Add Two Numbers 两数相加
18.1 Write a function that adds two numbers. You should not use + or any arithmetic operators. 这道题让我 ...
- LeetCode(2):Add Two Numbers 两数相加
Medium! 题目描述: 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头 ...
- 【LeetCode】2. Add Two Numbers 两数相加
给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和 ...
随机推荐
- Servlet监听器
一.servlet的8个监听器 场景 监听者接口 事件类型 你想知道一个web应用上下文中是否增加.删除或替换了一个属性 javax.servlet.ServletContextAttributeLi ...
- MVVM开发模式简单实例MVVM Demo
本文主要是翻译Rachel Lim的一篇有关MVVM模式介绍的博文 A Simple MVVM Example 并具体给出了一个简单的Demo(原文是以WPF开发的,对于我自己添加或修改的一部分会用红 ...
- css动画 animation
今天用css做了一个简单的三角上下移动的一个小动画,说白了就是在改变该物体的height值.除了这个方法,还可以用js. 一.在用css写动画时,一定要记住兼容性问题.如何解决该兼容性?在前面加内核前 ...
- Windows7 + Ubuntu双系统安装过程记录
本文为在已安装Windows7系统的前提下安装Ubuntu Kylin 14.10系统的过程以及期间出现的各种问题的记录. Ubuntu系统下载 Ubuntu Kylin中文官方网站:http://w ...
- 获取上个页面的url包括参数
Uri uri = Request.UrlReferrer; string path = uri.AbsoluteUri; TempData["path"] = path;
- Java的native方法
一. 什么是Native Method 简单地讲,一个Native Method就是一个java调用非java代码的接口.一个Native Method是这样一个java的方法:该方法的实现由非j ...
- RobotFrameWork(二)Ride简单使用及快捷键
一.简单示例 注意:以下操作使用到快捷键的,请先确保没有与其他软件的快捷键设置冲突,比如sogou拼音.有道词典等等 1.启动ride 启动ride方法: 1) 通过界面图标 2) dos命令行: ...
- springMVC学习之接受JSON参数
今天在springmvc使用rest模式异步提交,后台接受json字符.发现好多问题,感觉和spring3.0使用习惯上多少有点区别.因此把4.0的异步提交和方式记录下来. 前台页面代码如下: < ...
- winrt简单克隆对象
public MapPoint Copy()//MapPoint克隆方法 { MapPoint p = new MapPoint();//这是我自定义的对象 //利用反射获得类成员 FieldInfo ...
- ABAP 订单转交货单
*& Report ZSDR025 *& *&---------------------------------------------------------------- ...