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.  
Example
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
 
/**
* class ListNode {
* public int value;
* public ListNode next;
* public ListNode(int value) {
* this.value = value;
* next = null;
* }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
// Write your solution here
ListNode dummy = new ListNode(0);
ListNode cur = dummy;
int sum = 0;
while (l1 != null || l2 != null) {
if (l1 != null) {
sum += l1.value;
l1 = l1.next;
}
if (l2 != null) {
sum += l2.value;
l2 = l2.next;
}
cur.next = new ListNode(sum % 10);
cur = cur.next;
sum = sum / 10;
}
if (sum == 1) {
cur.next = new ListNode(1);
}
return dummy.next;
}
}

[Algo] 223. Add Two Numbers的更多相关文章

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

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

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

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

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

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

  4. Leetcode-2 Add Two Numbers

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

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

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

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

  7. LeetCode Add Two Numbers II

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

  8. [LeetCode_2] Add Two Numbers

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

  9. Two Sum & Add Two Numbers

    Two Sum 题目:https://leetcode.com/problems/two-sum/ class Solution(object): def twoSum(self, nums, tar ...

随机推荐

  1. Neo4j--UNIQUE约束

    UNIQUE简介 和关系型数据库一样,对数据进行约束作用. 比如在某个属性上不能插入重复的节点. 比如属性的完整性约束. 创建UNIQUE约束 创建UNIQUE语法 CREATE CONSTRAINT ...

  2. html通配符

    ♠ ♠ ♠ 黑桃 ♣ ♣ ♣ 梅花 ♥ ♥ ♥ 红桃,心 ♦ ♦ ♦ 方块牌 ◊ ◊ ◊ 菱形 † † † 匕首 ‡ ‡ ‡ 双剑号 ¡ ¡ ¡ 反向感叹号 ¿ ¿ ¿ 反向问号 ← ← ← 左箭头 ...

  3. html特殊字符的写法

    符号 写法 (空格)   <(小于号) < >(大于号) > " " ®(已注册) ® ©(版权) © ™(商标) ™ (半方大的空白)   (全方大的空白 ...

  4. SqlServer体系结构

    1.SQL的ABC 特色 (1) Application 应用:理念是提供软件.硬件.服务在内的完整解决方案 (2) Box盒子: 传统部署方式.部署在企业内部. (3)Cloud: 私有云 .公有云 ...

  5. A. Yellow Cards ( Codeforces Round #585 (Div. 2) 思维水题

    ---恢复内容开始--- output standard output The final match of the Berland Football Cup has been held recent ...

  6. 前端框架vue学习笔记

    占坑

  7. For循环的几个练习

    1.括号里面只能放加或减,如果要使等式成立,括号里面应该放什么运算符12()34()56()78()9 = 59 2.蓝球弹起的高度篮球从10米高的地方落下,每次弹起的高度是原来的0.3倍,问弹跳10 ...

  8. Spring Cloud Alibaba 教程 | 前世今生

    Spring Cloud Alibaba是什么 先来看一下官方是怎么定义Spring Cloud Alibaba的: Spring Cloud Alibaba 致力于提供微服务开发的一站式解决方案.此 ...

  9. UVA 11987 - Almost Union-Find 并查集的活用 id化查找

    受教了,感谢玉斌大神的博客. 这道题最难的地方就是操作2,将一个集合中的一个点单独移到另一个集合,因为并查集的性质,如果该点本身作为root节点的话,怎么保证其他点不受影响. 玉斌大神的思路很厉害,受 ...

  10. python学习笔记-模块和包

    模块导入方法 1.import 语句 import module1[,module2[,...moduleN]] 当我们使用import语句的时候,Python解释器是怎么找到对应对文件对呢?答案是解 ...