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 ...
随机推荐
- Codeforces-348E Pilgrims
#4342. CF348 Pilgrims 此题同UOJ#11 ydc的大树 Online Judge:Bzoj-4342,Codeforces-348E,Luogu-CF348E,Uoj-#11 L ...
- WPF 导出Excel 导出图片
/// <summary> /// 导出Excel /// </summary> private void ExportExcel(DataTable ExcelDt) { / ...
- spring中关于<context:component-scan>的使用说明
通常情况下我们在创建spring项目的时候在xml配置文件中都会配置这个标签,配置完这个标签后,spring就会去自动扫描base-package对应的路径或者该路径的子包下面的java文件,如果扫描 ...
- join和os.path.join 的用法
Python中有join和os.path.join()两个函数,具体作用如下: join:连接字符串数组.将字符串.元组.列表中的元素以指定的字符(分隔符)连接生成一个新的字符串os.path.joi ...
- Nonsense Time
Nonsense Time 时间限制: 10 Sec 内存限制: 128 MB 题目描述 You a given a permutation p1,p2,…,pn of size n. Initia ...
- mysqldump mysql数据库导出命令
mysqldump -u用户名 -p密码 数据库名 > 导出的文件名 例如: mysqldump -uroot -p123456 test > /var/test.sql 如果要压缩就用管 ...
- JavaScript中this的指向(转载)
转载自:http://www.cnblogs.com/dongcanliang/p/7054176.html 前言 this 指向问题是入坑前端必须了解知识点,现在迎来了ES6时代,因为箭头函数的出现 ...
- python_数据类型_list
names = ['one','two','three','four','five'] #列表切片 print(names[0:]) #['one', 'two', 'three', 'four', ...
- 用CSS添加选中文字的背景色
- ssh连接超时中断问题解决方案
当在终端使用ssh命令连接到服务器时,如果一段时间没有活动连接会被中断,以下有两种方案可以解决: 一.修改ssh客户端配置 编辑客户端 /etc/ssh/ssh_config (或~/.ssh/con ...