LeetCode第二题—— Add Two Numbers(模拟两数相加)
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(模拟两数相加)的更多相关文章
- leetcode 第二题Add Two Numbers java
链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...
- 445 Add Two Numbers II 两数相加 II
给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个新的链表.你可以假设除了数字 0 之外,这两个数字都不会以零开头.进阶:如果输入链表 ...
- LeetCode 第二题 Add Two Numbers 大整数加法 高精度加法 链表
题意 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...
- LeetCode 445. Add Two Numbers II (两数相加 II)
题目标签:Linked List 题目给了我们两个 数字的linked list,让我们把它们相加,返回一个新的linked list. 因为题目要求不能 reverse,可以把 两个list 的数字 ...
- [leetcode]445. Add Two Numbers II 两数相加II
You are given two non-empty linked lists representing two non-negative integers. The most significan ...
- 2. Add Two Numbers[M]两数相加
题目 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...
- Leetcode算法系列(链表)之两数相加
Leetcode算法系列(链表)之两数相加 难度:中等给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字.如果,我们将 ...
- 【LeetCode每日一题 Day 2】2. 两数相加
大家好,我是编程熊,今天是LeetCode每日一题的第二天,一起学习的是LeetCode第二题<两数相加>. 题意 给你两个 非空 的链表,表示两个非负的整数.它们每位数字都是按照 逆序 ...
- LeetCode第一题—— Two Sum(寻找两数,要求和为target)
题目描述: Given an array of integers, return indices of the two numbers such that they add up to a speci ...
随机推荐
- java读取pdf文本转换html
补充:一下代码基于maven,现将依赖的jar包单独导出 地址:pdf jar 完整代码地址 也就两个文件 java读取pdf中的纯文字,这里使用的是pdfbox工具包 maven引入如下配置 < ...
- <软件测试>软件测试
1.软件测试基础 软件测试工程师:查找错误和缺陷,然后要求开发人员进行修改,保证软件质量. 漏洞(360安全漏洞):硬件,软件,协议的具体实现或系统安全策略存在缺陷,从而可以使攻击者在未授权的情况下破 ...
- Mybatis Generator 安装(idea+maven)
1.在Intellij IDEA创建maven项目(本过程比较简单,略) 2. 在maven项目的pom.xml 添加mybatis-generator-maven-plugin 插件 <bui ...
- SYSTEM_HANDLE_TABLE_ENTRY_INFO
typedef struct _SYSTEM_HANDLE_TABLE_ENTRY_INFO { USHORT UniqueProcessId; USHORT CreatorBackTraceInde ...
- UNLISTEN - 停止监听通知信息
SYNOPSIS UNLISTEN { name | * } DESCRIPTION 描述 UNLISTEN 用于删除一个现有的已注册的 NOTIFY 事件. UNLISTEN 取消当前 Postgr ...
- ros机器人导航设置原点,目标点
之前利用movebase导航定位都是通过rviz用鼠标指来指去,实验时非常方便,但实际应用总不能也人工指来指去吧,这怎么体现智能呢 启动导航后,用以前使用的rviz设设置目标点来获取map坐标系下的位 ...
- VBA+SQL transform pivot union联合查询的基础应用
Sub 项目状态查询() '如果“项目状态”是未转运营那么实施状态是不能选择的,因为还没有实施.'如果“项目状态”选择状态后,那么项目名称里面只显示该状态的项目名称.如果“项目状态”选择的不是未转运营 ...
- UVA11427 Expect the Expected 概率dp+全概率公式
题目传送门 题意:小明每晚都玩游戏,每一盘赢的概率都是p,如果第一盘就赢了,那么就去睡觉,第二天继续玩:否则继续玩,玩到赢的比例大于p才去睡:如果一直玩了n盘还没完成,就再也不玩了:问他玩游戏天数的期 ...
- Hadoop–Task 相关
在MapReduce计算框架中,一个应用程序被划分为Map和Reduce两个计算阶段.他们分别由一个或多个Map Task 和Reduce Task组成. Map Task: 处理输入数据集合中的一片 ...
- 改变this 指向的3种方法
1.在函数内部声明一个that,然后将this赋值给that, var that=this; 最后用that 代替this使用 <!DOCTYPE html> <html lang= ...