Leetcode2.Add Two Numbers两数相加
给定两个非空链表来表示两个非负整数。位数按照逆序方式存储,它们的每个节点只存储单个数字。将两数相加返回一个新的链表。
你可以假设除了数字 0 之外,这两个数字都不会以零开头。
示例:
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4) 输出:7 -> 0 -> 8 原因:342 + 465 = 807
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2)
{
int x = 0;
ListNode* head = NULL;
ListNode* last = NULL;
while(l1 && l2)
{
int temp = l1 ->val + l2 ->val + x;
x = temp / 10;
temp = temp % 10;
if(head == NULL)
{
head = new ListNode(temp);
last = head;
}
else
{
last ->next = new ListNode(temp);
last = last ->next;
}
l1 = l1 ->next;
l2 = l2 ->next;
}
while(l1)
{
int temp = l1 ->val + x;
x = temp / 10;
temp = temp % 10;
if(head == NULL)
{
head = new ListNode(temp);
last = head;
}
else
{
last ->next = new ListNode(temp);
last = last ->next;
}
l1 = l1 ->next;
}
while(l2)
{
int temp = l2 ->val + x;
x = temp / 10;
temp = temp % 10;
if(head == NULL)
{
head = new ListNode(temp);
last = head;
}
else
{
last ->next = new ListNode(temp);
last = last ->next;
}
l2 = l2 ->next;
}
if(x != 0)
{
last ->next = new ListNode(x);
last = last ->next;
}
return head;
}
};
Leetcode2.Add Two Numbers两数相加的更多相关文章
- [CareerCup] 18.1 Add Two Numbers 两数相加
18.1 Write a function that adds two numbers. You should not use + or any arithmetic operators. 这道题让我 ...
- LeetCode(2):Add Two Numbers 两数相加
Medium! 题目描述: 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头 ...
- 【LeetCode】Add Two Numbers(两数相加)
这道题是LeetCode里的第2道题. 题目要求: 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将 ...
- [LeetCode]2.Add Two Numbers 两数相加(Java)
原题地址: add-two-numbers 题目描述: 给你两个非空的链表,表示两个非负的整数.它们每位数字都是按照逆序的方式存储的,并且每个节点只能存储一位数字. 请你将两个数相加,并以相同形式返回 ...
- 【LeetCode】2. Add Two Numbers 两数相加
给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和 ...
- [leetcode]2. Add Two Numbers两数相加
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- LeetCode 2. Add Two Numbers (两数相加)
题目标签:Linked List, Math 题目给了我们两个 Linked List, 各代表一个数字,不过顺序的反的.让我们把两个数字相加. 和普通的相加其实差不多,只不过变成了 Linked L ...
- 【LeetCode每天一题】Add Two Numbers(两链表相加)
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- [Leetcode] Add two numbers 两数之和
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
随机推荐
- PKUWC 2018 真实排名
PKUWC2018 真实排名 题面描述 共有\(n\)个人,每个人有一个能力值,每个人的排名为所有能力值不比他小的人的个数(包括他自己). 现在有\(k\)个人能力值翻倍,但我们无法得知是哪\(k\) ...
- Linux跨PC拷贝之SCP
命令:scp 不同的Linux之间copy文件常用有3种方法: 第一种就是ftp,也就是其中一台Linux安装ftp Server,这样可以另外一台使用ftp的client程序来进行文件的copy. ...
- spfa模版
#include<bits/stdc++.h> using namespace std; int n,m;//点边 int beginn; ],v[],w[]; ],nextt[]; ]; ...
- Creating a bootable Ubuntu USB stick
Windows: https://tutorials.ubuntu.com/tutorial/tutorial-create-a-usb-stick-on-windows#0 Ubuntu: http ...
- 第五章 Odoo 12开发之导入、导出以及模块数据
大多数Odoo 模块的定义,如用户界面和安全规则,实际是存储在对应数据表中的数据记录.模块中的 XML 和 CSV 文件不是 Odoo 应用运行时使用,而是载入数据表的手段.正是因为这个原因,Odoo ...
- Intelij Idea 2016破解
在注册时选择License server,输入http://www.iteblog.com/idea/key.php,点击OK
- Java虚拟机系列(二)---HotSpot虚拟机对象
都知道Java是一门面向对象的编程语言,在Java程序运行过程中,无时无刻不在创建对象,所以这节来总结一下HotSpot虚拟机中的Java对象. 一.Java虚拟机对象的创建过程. 在语义层面创建一个 ...
- 爬虫的终极形态:nightmare
爬虫的终极形态:nightmare nightmare 是一个基于 electron 的自动化库(意思是说它自带浏览器),用于实现爬虫或自动化测试.相较于传统的爬虫框架(scrapy/pyspider ...
- 简单linux查询
1查看日志异常 tailf nohup.out|grep ERROR -A 3 --color tailf nohup.out|grep ERROR|grep chunk -A 3 -B 3 -- ...
- Spring学习:程序的耦合和解耦的思路分析
程序的耦合 耦合:程序间的依赖关系 包括: 类之间的依赖 方法间的依赖 解耦: 降低程序间的依赖关系 在实际开发中: 应该做到,编译期不依赖,运行时才依赖 解耦思路: 第一步:使用反射来创建对象,而避 ...