[leetcode]2. Add Two Numbers两数相加
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.
题目:
用两个链表表示的两个数,求相加之和
Solution1: For given two lists, keep looping where either one isn't null. When ListNode comes to null, consider its val as 0.
For result list, use a dummy node.

code:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
/*
Time Complexity: O(max(l1,l2))
Space Complexity: O(max(l1,l2))
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(-1);
ListNode pre = dummy;
ListNode p1 = l1;
ListNode p2 = l2;
int carry = 0;
while(p1 != null || p2 != null){
int x = (p1 != null) ? p1.val : 0;
int y = (p2 != null) ? p2.val : 0;
int sum = carry + x + y;
carry = sum / 10;
// generate the result list
pre.next = new ListNode(sum % 10);
// move three lists pointers
pre = pre.next;
if(p1 != null) p1 = p1.next;
if(p2 != null) p2 = p2.next;
}
// in case last two digit sum > 10
if(carry > 0){
pre.next = new ListNode(carry);
}
return dummy.next;
}
}
[leetcode]2. Add Two Numbers两数相加的更多相关文章
- 【LeetCode】Add Two Numbers(两数相加)
这道题是LeetCode里的第2道题. 题目要求: 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将 ...
- [LeetCode]2.Add Two Numbers 两数相加(Java)
原题地址: add-two-numbers 题目描述: 给你两个非空的链表,表示两个非负的整数.它们每位数字都是按照逆序的方式存储的,并且每个节点只能存储一位数字. 请你将两个数相加,并以相同形式返回 ...
- LeetCode 2. Add Two Numbers (两数相加)
题目标签:Linked List, Math 题目给了我们两个 Linked List, 各代表一个数字,不过顺序的反的.让我们把两个数字相加. 和普通的相加其实差不多,只不过变成了 Linked L ...
- LeetCode(2):Add Two Numbers 两数相加
Medium! 题目描述: 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头 ...
- [CareerCup] 18.1 Add Two Numbers 两数相加
18.1 Write a function that adds two numbers. You should not use + or any arithmetic operators. 这道题让我 ...
- Leetcode2.Add Two Numbers两数相加
给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 -& ...
- 【LeetCode】2. Add Two Numbers 两数相加
给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和 ...
- LeetCode(2): 两数相加
本内容为LeetCode第二道题目:两数相加 # -*- coding: utf-8 -*- """ Created on Sun Mar 10 10:47:12 201 ...
- [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现
[LeetCode] Add Two Numbers 两个数字相加 You are given two non-empty linked lists representing two non-ne ...
随机推荐
- shell脚本使用## or %%
今天写脚本的时候,遇到一个文件路径需要去掉右边一部分,当时就想到了这个,但是很久没用过了,很多不记得了,记录一下这种用法 1:vim test.sh #!/bin/bash location=/f ...
- SQL查:询结果区分大小写
在MS SQL2005中的方法: 1)select * from user where name collate Chinese_PRC_CS_AS like 'A$%B%' escape '$'; ...
- MDX函数
MDX重点函数 成员函数 1..CurrentMember 获取运行时当前的成员,用法:<Dimension>.CurrentMember . 2..Parent 获取运行时当前的成员的父 ...
- [UE4]Input Key Selector
一.Input Key Selector:按键设置 二.On Key Selected:按键收集完毕后触发该事件. 三.点击On Key Selected控件后,会显示“...”,表示可以接受键盘输入 ...
- 涂抹mysql笔记-InnoDB/MyISAM及其它各种存储引擎
存储引擎:一种设计的存取和处理方式.为不同访问特点的表对象指定不同的存储引擎,可以获取更高的性能和处理数据的灵活性.通常是.so文件,可以用mysql命令加载它. 查看当前mysql数据库支持的存储引 ...
- CodeSmith和Powerdesigner的搭建和实例化操作 转载自黄聪同学
好了,废话少说,开始我们的CodeSmith旅程吧,我先讲讲这个系列教程要完成的目标吧,众所周知,CodeSmith其中一个强大的功能就是依照模板生成批量代码,这也是吸引着众多编程人士使用它的原因,它 ...
- Dubbo基本原理机制
分布式服务框架: –高性能和透明化的RPC远程服务调用方案 –SOA服务治理方案 -Apache MINA 框架基于Reactor模型通信框架,基于tcp长连接 Dubbo缺省协议采用单一长连接和 ...
- 小程序获取微信用户的openid
小程序获取微信用户的openid //index.js //获取应用实例 const app = getApp() Page({ globalData: { appid: '11121221a89e0 ...
- leetcode96
class Solution { public: int numTrees(int n) { vector<,); f[]=; f[]=; ;i<=n;i++){ ;j<=i;j++ ...
- React将某段文字插入到某个元素里
最基本使用: 引入依赖文件: <script src="https://unpkg.com/react@16/umd/react.development.js">< ...