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. BGP(IBGP“内部路由器”和EBGP“外部路由器”)命令解析

    BGP:基于策略的路径向量路由协议. ①:(attribute)属性描述路径. ②:使用TCP(端口179)作为传输协议——(IBGP多使用loopback端口建立update-source) IBG ...

  2. NCRE Java二级备考方案

    一 配合大纲梳理基本知识点 二 多在233网校刷题

  3. Sqlserver 增删改查----增

    注意我说的常见查询,可不是简单到一个表得增删改查,做过实际开发得人都知道,在实际开发中,真正牵扯到一个表得增删改查只能说占很小得一部分,大多都是好几个表的关联操作的. 下面我就说一下我在实际开发中经常 ...

  4. web嵌入到原生的app里需要注意的事项

    1.https://www.cnblogs.com/shimily/articles/7943370.html 2.https://www.cnblogs.com/stoneniqiu/p/60771 ...

  5. Navicat mysql 数据库备份和使用,备份以后是nb3文件

    通过Navicat进行Mysql数据库自动备份与还原   Mysql数据库自动备份流程 Navicat版本为:Navicat 12.0.26 例:test为用于测试自动备份的数据库,里面有表t_per ...

  6. WebSocket的简单实现&jsp

    创建一个web项目 导入依赖: <?xml version="1.0" encoding="UTF-8"?> <project xmlns=& ...

  7. UVA 12663 第九届省赛 高桥与低桥 线段树

    题意很简单,n个桥的高度是事先给出来的,然后有m次涨水与落水的高度,问有多少座桥在这m次涨落之后 被淹超过了k次,如果某桥本身被水淹了,此时再涨水,就不能算多淹一次 看下数据10的五次方,10的五次方 ...

  8. 吴裕雄--天生自然Django框架开发笔记:Django简介

    Python下有许多款不同的 Web 框架.Django是重量级选手中最有代表性的一位.许多成功的网站和APP都基于Django. Django是一个开放源代码的Web应用框架,由Python写成. ...

  9. c++ 获取GMT 时间和字符串

    需要跨平台,所以可选的只有std 和 boost: boost 比较复杂了 #include <boost/date_time/local_time/local_time.hpp> std ...

  10. XML技术详解

    XML 1.XML概述 XML可扩展标记语言是一种基于文本的语言用作应用程序之间的通信模式,是一个非常有用的描述结构化信息的技术.XML工具使得转化和处理数据变得十分容易,但同样也要领域相关的标准和代 ...