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 232C Doe Graphs(分治+搜索)
CF232C Doe Graphs 题意 题意翻译 \(Doe\)以她自己的名字来命名下面的无向图 \(D(0)\)是只有一个编号为\(1\)的结点的图. \(D(1)\)是只有两个编号分别为\(1\ ...
- windows API 第八篇 _tcsicmp _stricmp _wcsicmp _mbsicmp
这些函数都是比较字符串小写的,忽略大写,出入的字符串都将按照小写比较Perform a lowercase comparison of strings. 函数原型: int _stricmp( con ...
- vue.js_11_路由的2中参数传递和路由的嵌套
1.以?的形式传递参数 <router-link to="/login?id=10&name=zs">登录</router-link> 发送参数 ...
- 构建支持中文字体的moviepy镜像
首先是系统的环境问题. linux 安装 moviepy需要很多依赖,安装起来费神费力.配置起来也非常麻烦,最简单的办法是直接使用他人构建好的镜像文件. 再就是字体显示问题. 镜像中的imagmagi ...
- Hadoop生态系统概况(转)图文并茂说的不错
Hadoop是一个能够对大量数据进行分布式处理的软件框架.具有可靠.高效.可伸缩的特点. Hadoop的核心是HDFS和Mapreduce,hadoop2.0还包括YARN. 下图为hadoop的生态 ...
- Hadoop IO 特性详解(2)【文件校验】
(本文引用了microheart,ggjucheng的一些资料,在此感谢.charles觉得知识无价,开源共享无价) 这一次我们接着分析文件IO校验的相关代码,看看最底层是如何实现这种大数据集的文件校 ...
- 关于Python3 打印中文乱码问题
解决方案有两种: 在命令行前指定编码 $ PYTHONIOENCODING=utf-8 python test.py hello world 你好,世界 在代码中指定编码 import io impo ...
- kuangbin带我飞QAQ 最短路
1. poj 1502 Mathches Game 裸最短路 #include <iostream> #include <string.h> #include <cstd ...
- img标签中的onerror事件
img标签中有一个onerror事件.是当我引用的src属性获取不到图片或者网络错误导致无法正常显示我src属性的图片时,显示的提示错误图片或者是可以代替的万能图片. 用法: <img src= ...
- jQuery控制导航条样式
原理:点击当前元素时,当前元素添加(样式类),父辈的兄弟姐妹的孩子('a')去掉此样式类. 代码如下: /*次要导航*/ $(".subnav li a").click(funct ...