给定两个非空链表来表示两个非负整数。位数按照逆序方式存储,它们的每个节点只存储单个数字。将两数相加返回一个新的链表。

你可以假设除了数字 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两数相加的更多相关文章

  1. [CareerCup] 18.1 Add Two Numbers 两数相加

    18.1 Write a function that adds two numbers. You should not use + or any arithmetic operators. 这道题让我 ...

  2. LeetCode(2):Add Two Numbers 两数相加

    Medium! 题目描述: 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头 ...

  3. 【LeetCode】Add Two Numbers(两数相加)

    这道题是LeetCode里的第2道题. 题目要求: 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将 ...

  4. [LeetCode]2.Add Two Numbers 两数相加(Java)

    原题地址: add-two-numbers 题目描述: 给你两个非空的链表,表示两个非负的整数.它们每位数字都是按照逆序的方式存储的,并且每个节点只能存储一位数字. 请你将两个数相加,并以相同形式返回 ...

  5. 【LeetCode】2. Add Two Numbers 两数相加

    给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和 ...

  6. [leetcode]2. Add Two Numbers两数相加

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

  7. LeetCode 2. Add Two Numbers (两数相加)

    题目标签:Linked List, Math 题目给了我们两个 Linked List, 各代表一个数字,不过顺序的反的.让我们把两个数字相加. 和普通的相加其实差不多,只不过变成了 Linked L ...

  8. 【LeetCode每天一题】Add Two Numbers(两链表相加)

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

  9. [Leetcode] Add two numbers 两数之和

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

随机推荐

  1. Mybatis的插件 PageHelper 分页查询使用方法

    参考:https://blog.csdn.net/ckc_666/article/details/79257028 Mybatis的一个插件,PageHelper,非常方便mybatis分页查询,国内 ...

  2. python内置类型详细解释

    文章编写借鉴于内置类型 - Python 3.7.3 文档,主要用于自己学习和记录 python主要内置类型包括数字.序列.映射.类.实例和异常 有些多项集类是可变的.它们用于添加.移除或重排其成员的 ...

  3. hammer.js使用

    手势包括点击(tap),长按(press),滑动(swipe),方向(pan) 使用实例: <!DOCTYPE html> <html> <head> <me ...

  4. HZOI20190803 B题

    题目:https://www.cnblogs.com/Juve/articles/11295333.html 话说这题方法挺多 40分:暴力 65:莫队,你会T得飞起 我考场上没打出带修莫队,没有修改 ...

  5. 装配SpringBean(四)--注解装配之组件扫描

    前两篇文章我总结了通过XML方式装配bean的实现方式,虽然比较简单,但是需要配置很多,很多时候我们都会使用注解进行装配.使用注解的方式可以减少XML的配置,既能实现XML的功能,还提供了自动装配功能 ...

  6. 【DM8168学习笔记5】EZSDK目录结构

    EZSDK5.02的目录结构与之前的版本不同,之前的版本各个组件都放在/ezsdk目录下,5.02做了整合. 之前版本:(图片摘自:3.DM816x_1-day_Workshop-Getting_St ...

  7. [转载] OpenCV2.4.3 CheatSheet学习(一)

    OpenCV向MATLAB靠拢,图像的操作方法变得不那么C了,更m了一些.比如,MATLAB中的常用函数imshow.imread.imwrite函数在OpenCV中已经有了同名的兄弟. 此外,Ope ...

  8. jmeter-监听器介绍与使用

    12.jmeter-监听器介绍与使用 jmeter-监听器介绍与使用 察看结果树 Summary Report 聚合报告 Backend Listener Aggregate Graph 断言结果 C ...

  9. Leetcode437Path Sum III路径总和3

    给定一个二叉树,它的每个结点都存放着一个整数值. 找出路径和等于给定数值的路径总数. 路径不需要从根节点开始,也不需要在叶子节点结束,但是路径方向必须是向下的(只能从父节点到子节点). 二叉树不超过1 ...

  10. Hackerrank--Ashton and String(后缀数组)

    题目链接 Ashton appeared for a job interview and is asked the following question. Arrange all the distin ...