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. 13 ~ express ~ 后台页面的搭建

    一, 后台路由文件  /router/admin.js  var express = require('express') var router = express.Router() /** * 验证 ...

  2. Toolbar中系统自带返回键(setDisplayHomeAsUpEnabled)与Toolbar本身的inflateMenu冲突问题

    在APP的Toolbar中可以设置inflateMenu来添加右上角菜单栏,但是我又同时想用系统自带的左侧返回键,随之也产生了问题:右上角的inflateMenu会不生效,即不会产生菜单按钮,查阅资料 ...

  3. Spring(5) -(14) pointcut 语法

    AOP的规范本应该由SUM公司提出,但是被AOP联盟捷足先登.AOP联盟指定AOP规范,首先就要解决一个问题,怎么表示切入点,也就是在哪些方法上增强(where) AspectJ 是一个面向切面的框架 ...

  4. Codeforces_448C 分治

    昨晚CF碰到的题目,昨晚CF跪了啊啊啊 题意比较简单,给定一排挨在一起的板子,宽度都为1,高度不一,一个刷子宽度也是1,可以横着刷,也可以竖着刷,但是任何时刻刷子都要在板子上,也就是说,如果横向的时候 ...

  5. 洛谷-P2634 [国家集训队]聪聪可可 点分治

    Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一般情况下石头剪刀布就好 ...

  6. HZNU-ACM寒假集训Day7小结 背包DP

    背包问题 01背包 状态:f(i,j) 表示只能装前i个物品的情况下,容量为j的背包所能达到的最大总价值 状态转移方程:  f(i,j)=max(f(i-1,j),f(i-1,j-w[i])+v[i] ...

  7. NCRE的JAVA二级考试大纲

    全国计算机等级考试二级 Java 语言 程序设计考试大纲(2018 年版) 基本要求 1. 掌握 Java 语言的特点.实现机制和体系结构. 2. 掌握 Java 语言中面向对象的特性. 3. 掌握 ...

  8. git本地代码回滚

    git本地操作有时会有误删误改并提交的时候,此时可以通过回滚获得之前的特定版本 1. 查看log 输入  git log  查看commit记录 [xxxxxxx]$ git log 结果大致如下(根 ...

  9. IDEA--IDEA配置web项目

    参考:https://blog.csdn.net/kfm1376822651/article/details/79666586 记学习springmvc时,使用idea部署web项目至tomcat. ...

  10. C语言I作业博客07

    这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/CST2019-1/homework/9935 我在这个课程的目 ...