一、question

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: ( ->  -> ) + ( ->  -> )
Output: -> ->
Explanation: + = .

二、solution

(cpp)

/**
* 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 preHead(), *p = &preHead;
int carry=;
while(l1||l2||carry){
int sum = (l1?l1->val:)+(l2?l2->val:)+carry;
carry = sum/;
p->next = new ListNode(sum % );
p = p->next;
l1 = l1?l1->next:;
l2 = l2?l2->next:;
}
return preHead.next;
}
};

(js)

/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/ var addTwoNumbers = function(l1, l2) {
var add = 0
, ans
, head; while(l1 || l2) {
var a = l1 ? l1.val : 0
, b = l2 ? l2.val : 0; var sum = a + b + add;
add = ~~(sum / 10); var node = new ListNode(sum % 10); if (!ans)
ans = head = node;
else {
head.next = node;
head = node;
} if (l1)
l1 = l1.next;
if (l2)
l2 = l2.next;
} if (add) {
var node = new ListNode(add);
head.next = node;
head = node;
} return ans;
};

Algorithm——Add Two Numbers(补上周)的更多相关文章

  1. [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 ...

  2. LeetCode: Add Two Numbers 解题报告

    Add Two NumbersYou are given two linked lists representing two non-negative numbers. The digits are ...

  3. [LeetCode] Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  4. [LeetCode] Add Two Numbers 两个数字相加

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  5. 52. 不用+、-、×、÷做加法[add two numbers without arithmetic]

    [本文链接] http://www.cnblogs.com/hellogiser/p/add-two-numbers-without-arithmetic.html [题目] 写一个函数,求两个整数的 ...

  6. Leetcode-2 Add Two Numbers

    #2. Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits ...

  7. [LintCode] Add Two Numbers 两个数字相加

    You have two numbers represented by a linked list, where each node contains a single digit. The digi ...

  8. LeetCode Add Two Numbers II

    原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...

  9. [LeetCode_2] Add Two Numbers

    LeetCode: 2. Add Two Numbers /** * Definition for singly-linked list. * struct ListNode { * int val; ...

随机推荐

  1. 怎么在eclipse中访问webservice

    在eclipse创建webservice的方法: 1.在Eclipse的菜单栏中,Window --> Preferences --> Web Service --> Axis2 P ...

  2. java API连接虚拟机上的hbase

    今天用本地的eclipse连接虚拟机上的hbase数据库,代码如下: public static void main(String[] args) throws Exception{ Configur ...

  3. XP系统安装VS2010失败的解决方法

    今天装了一个双系统,但是在XP系统上安装VS2010的时候就出现了下面的错误 于是在网上搜索各种资源,查看错误日志,网上说是office2007的原因,我也抱着试试看的态度去卸载了,可是卸载后却发现还 ...

  4. 用Python玩转数据第六周——高级数据处理与可视化

    1.matplotlib中有两个模块,pyplot和pylab import matplotlib.pyplot as plt  ///plt.plot(x,y) import pylab as pl ...

  5. (转)mysql5.7 根据二进制文件mysqlbinlog恢复数据库 Linux

    原文:http://blog.csdn.net/qq_15058425/article/details/61196085 1.开始mysqlbinlog日志功能 先找打my.cnf文件的位置: 2.编 ...

  6. python strip()函数的用法

    函数原型 声明:s为字符串,rm为要删除的字符序列 s.strip(rm)         删除s字符串中开头.结尾处,位于 rm删除序列的字符 s.lstrip(rm)        删除s字符串中 ...

  7. Spring Boot 日志配置

    Spring Boot 日志配置 默认日志 Logback: 默认情况下,Spring Boot会用Logback来记录日志,并用INFO级别输出到控制台.在运行应用程序和其他例子时,你应该已经看到很 ...

  8. HUE配置文件hue.ini 的database模块详解(包含qlite、mysql、 psql、和oracle)(图文详解)(分HA集群和非HA集群)

    不多说,直接上干货! Hue配置文件里,提及到,提供有postgresql_psycopg2, mysql, sqlite3 or oracle. 注意:Hue本身用到的是sqlite3. 在哪里呢, ...

  9. mongodb-手写mongoclient加入到springmvc中

    由于一个项目使用的是springmvc3.x版本, mongodb使用的为3.x版本, 所以springmvc继承的mongodb-data并不可用, 只能自己手写一个mongoclient并加入到s ...

  10. 面试题-----ICMP协议简介

    ICMP协议简介 l  ICMP网际控制报文协议,通过它可以知道故障的具体原因和位置. l  由于IP不是为可靠传输服务设计的,ICMP的目的主要是用于在TCP/IP网络中发送出错和控制消息. l  ...