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. NRF52811-QCAA 蓝牙5.1芯片资料解析

    为了满足市场需求Nordic 宣布推出nRF52811系统级芯片(SoC),这个全功能无线连接解决方案支持蓝牙5.1 测向(Direction Finding)功能和一系列流行低功耗无线协议,用于智能 ...

  2. 19 01 18 dango 模型

    定义属性 Django根据属性的类型确定以下信息: 当前选择的数据库支持字段的类型 渲染管理表单时使用的默认html控件 在管理站点最低限度的验证 django会为表创建自动增长的主键列,每个模型只能 ...

  3. df 、dh

    查看磁盘 不挂载获取文件系统 [root@localhost ~]# file -s /dev/sda1/dev/sda1: SGI XFS filesystem data (blksz 4096, ...

  4. bzoj 4247挂饰

    背包????不会... #include<bits/stdc++.h> #define INF 0x7fffffff #define LL long long #define N 1000 ...

  5. 吴裕雄--天生自然 JAVASCRIPT开发学习:作用域

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. 深入理解Canvas Scaler

    Canvas Scaler: 这是一个理解起来相当繁琐复杂的一个组件,但又是一个至关重要的组件,不彻底了解它,可以说对UGUI的布局和所谓的“自适应”就没有一个完整的认识. Canvas Scale指 ...

  7. idea~创建maven webapp项目

    1.选择 org.apache.maven.archtypes:maven-archtype-webapp 2.禁止远程下载 archetypeCatalog=internal 目的是不远程下载,否则 ...

  8. c++程序—变量

    #include<iostream> using namespace std; int main() { int a = 10; cout << "a=" ...

  9. 2020/2/3 PHP代码审计之PHP伪协议

    0x00 简介 开局一张图233 0x01 file://协议 说明: file:// 文件系统是 PHP 使用的默认封装协议,展现了本地文件系统.当指定了一个相对路径(不以/..\或 Windows ...

  10. 使用 this 关键字定义方法和属性

    1.方法和属性的定义 属性是类中声明的变量,与其他地方变量的声明基本相同,只是属性必须 this 关键字,并且这里没有var 关键字. this.age; 在使用时,先是 this 关键字,之后的点语 ...