一、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. poj3070 Fibonacci(矩阵快速幂)

    矩阵快速幂基本应用. 对于矩阵乘法与递推式之间的关系: 如:在斐波那契数列之中 f[i] = 1*f[i-1]+1*f[i-2]  f[i-1] = 1*f[i-1] + 0*f[i-2].即 所以, ...

  2. sublime text3---Emmet:HTML/CSS代码快速编写神器

    Emmet的前身是大名鼎鼎的Zen coding,如果你从事Web前端开发的话,对该插件一定不会陌生.它使用仿CSS选择器的语法来生成代码,大大提高了HTML/CSS代码编写的速度,比如下面的演示: ...

  3. D05——C语言基础学PYTHON

    C语言基础学习PYTHON——基础学习D05 20180815内容纲要: 1 模块 2 包 3 import的本质 4 内置模块详解 (1)time&datetime (2)datetime ...

  4. struts2中s:select标签的使用

    1.第一个例子: <s:select list="{'aa','bb','cc'}" theme="simple" headerKey="00& ...

  5. 导出数据库数据成txt格式

    set verify off; set colsep ‘分隔符’; set echo off; set feedback off; set heading off; set pagesize 0; s ...

  6. Yum Proxy

    $ cat /etc/yum.conf[main]cachedir=/var/cache/yum/$basearch/$releaseverkeepcache=0debuglevel=2logfile ...

  7. 剑指offer二十三之二叉搜索树的后序遍历序列

    一.题目 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 二.思路 1.二叉搜索树又称二叉排序树(Binar ...

  8. ubuntu 安装JDK8

    1.下载JDK选择下载Linux版本-64位:jdk-8u65-linux-x64.tar.gz下载链接:http://www.oracle.com/technetwork/java/javase/d ...

  9. 【数组】Game of Life

    题目: According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a ce ...

  10. 全连接层(FC)与全局平均池化层(GAP)

    在卷积神经网络的最后,往往会出现一两层全连接层,全连接一般会把卷积输出的二维特征图转化成一维的一个向量,全连接层的每一个节点都与上一层每个节点连接,是把前一层的输出特征都综合起来,所以该层的权值参数是 ...