Description:

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: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

Solution

Approach 1: Elementary Math(2ms,45.9MB)

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummyHead = new ListNode(0);
ListNode p = l1, q = l2, curr = dummyHead;
int carry = 0;
while (p != null || q != null) {
int x = (p != null) ? p.val : 0;
int y = (q != null) ? q.val : 0;
int sum = carry + x + y;
carry = sum / 10;
curr.next = new ListNode(sum % 10);
curr = curr.next;
if (p != null) p = p.next;
if (q != null) q = q.next;
}
if (carry > 0) {
curr.next = new ListNode(carry);
}
return dummyHead.next;
}
}

LeetCode第二题—— Add Two Numbers(模拟两数相加)的更多相关文章

  1. leetcode 第二题Add Two Numbers java

    链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...

  2. 445 Add Two Numbers II 两数相加 II

    给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个新的链表.你可以假设除了数字 0 之外,这两个数字都不会以零开头.进阶:如果输入链表 ...

  3. LeetCode 第二题 Add Two Numbers 大整数加法 高精度加法 链表

    题意 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...

  4. LeetCode 445. Add Two Numbers II (两数相加 II)

    题目标签:Linked List 题目给了我们两个 数字的linked list,让我们把它们相加,返回一个新的linked list. 因为题目要求不能 reverse,可以把 两个list 的数字 ...

  5. [leetcode]445. Add Two Numbers II 两数相加II

    You are given two non-empty linked lists representing two non-negative integers. The most significan ...

  6. 2. Add Two Numbers[M]两数相加

    题目 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...

  7. Leetcode算法系列(链表)之两数相加

    Leetcode算法系列(链表)之两数相加 难度:中等给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字.如果,我们将 ...

  8. 【LeetCode每日一题 Day 2】2. 两数相加

    大家好,我是编程熊,今天是LeetCode每日一题的第二天,一起学习的是LeetCode第二题<两数相加>. 题意 给你两个 非空 的链表,表示两个非负的整数.它们每位数字都是按照 逆序 ...

  9. LeetCode第一题—— Two Sum(寻找两数,要求和为target)

    题目描述: Given an array of integers, return indices of the two numbers such that they add up to a speci ...

随机推荐

  1. MFS分布式文件系统【2】MFS MASTER 部署

    MFS版本 mfs-1.6.27 MFS-MASTER 192.168.1.190 MFS-CHUNKSERVER1 192.168.1.252 MFS-CHUNKSERVER2 192.168.1. ...

  2. 制作 U 盘启动盘

    制作 U 盘启动盘 这篇文章说的是 U 盘启动盘是如何运作的,同时有一个既能装 Windows 又能装 Linux 的 U 盘启动盘的例子. U 盘启动盘 当按下开机键后,电脑能启动我原本安装的操作系 ...

  3. [BOI2009]Radio Transmission 无线传输

    题目描述 给你一个字符串,它是由某个字符串不断自我连接形成的. 但是这个字符串是不确定的,现在只想知道它的最短长度是多少. 输入输出格式 输入格式: 第一行给出字符串的长度,1 < L ≤ 1, ...

  4. 2019_8_1python

    #函数 #函数是用来重复使用哒 #定义函数套路 ''' 1.首先要会写出裸代码,然后看看哪里是重复需要使用的 2.接下来将需要重复使用的代码转换成参数,带入到函数中 函数格式 def funcName ...

  5. codeforces750E New Year and Old Subsequence 矩阵dp + 线段树

    题目传送门 思路: 先看一个大牛的题解 题解里面对矩阵的构造已经写的很清楚了,其实就是因为在每个字符串都有固定的很多中状态,刚好可以用矩阵来表达,所以$(i,j)$这种状态可以通过两个相邻的矩阵的$m ...

  6. Tools: geos 使用指南

    1. 下载geos 2. 进入VS开发人员命令提示3.依次执行如下命令 >VCVARS32.BAT>cd D:\DevTool\geos-3.7.0>atuogen.bat>n ...

  7. 转载-------- JSON 与 对象 、集合 之间的转换 JSON字符串和java对象的互转【json-lib】

    转载--*--*---- 在开发过程中,经常需要和别的系统交换数据,数据交换的格式有XML.JSON等,JSON作为一个轻量级的数据格式比xml效率要高,XML需要很多的标签,这无疑占据了网络流量,J ...

  8. Python:Logging日志处理

    程序中,需要添加日志来记录大量信息. import logging # 第一步:创建logger self.logger = logging.getLogger() self.logger.setLe ...

  9. PHP ftp_site() 函数

    定义和用法 The ftp_site() 函数向 FTP 服务器发送 SITE 命令. SITE 命令没有标准化,在不同的服务器上不尽相同.对于处理文件权限或组关系方面的事情,SITE 命令很有用. ...

  10. NX二次开发-C++ CopyFile函数的用法

    NX9+VS2012 #include<Windows.h> CopyFile("D:\\test.prt","D:\\1\\test123.prt" ...