#2. Add Two Numbers

You are given two linked lists representing two non-negative numbers. 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.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* head=new ListNode();
ListNode* p=head;
ListNode* q=head; int carry=;
int sum=;
int all=;
while(l1!=NULL&&l2!=NULL)
{
all=l1->val+l2->val+carry;
sum=all%;
carry=all/;
p->val=sum;
p->next=new ListNode();
p=p->next;
l1=l1->next;
l2=l2->next;
} while(l1!=NULL)
{
all=l1->val+carry;
sum=all%;
carry=all/;
p->val=sum;
p->next=new ListNode();
p=p->next;
l1=l1->next;
}
while(l2!=NULL)
{
all=l2->val+carry;
sum=all%;
carry=all/;
p->val=sum;
p->next=new ListNode();
p=p->next;
l2=l2->next;
}
if(carry!=)
{
p->val=;
p->next=new ListNode();
p=p->next;
}
while(q!=NULL)
{
if(q->next->next==NULL)
{
q->next=NULL;
break;
}
q=q->next;
} return head; }
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* head=new ListNode();
ListNode* p=head; int carry=;
int sum=;
int all=;
while(l1!=NULL&&l2!=NULL)
{
all=l1->val+l2->val+carry;
sum=all%;
carry=all/;
p->next=new ListNode(sum);
p=p->next;
l1=l1->next;
l2=l2->next;
} while(l1!=NULL)
{
all=l1->val+carry;
sum=all%;
carry=all/;
p->next=new ListNode(sum);
p=p->next;
l1=l1->next;
}
while(l2!=NULL)
{
all=l2->val+carry;
sum=all%;
carry=all/;
p->next=new ListNode(sum);
p=p->next;
l2=l2->next;
}
if(carry!=)
{
p->next=new ListNode();
p=p->next;
} return head->next; }
};

Leetcode-2 Add Two Numbers的更多相关文章

  1. LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters

    LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...

  2. LeetCode:1. Add Two Numbers

    题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...

  3. [LeetCode] 445. Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  4. LeetCode 面试:Add Two Numbers

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

  5. LeetCode #002# Add Two Numbers(js描述)

    索引 思路1:基本加法规则 思路2:移花接木法... 问题描述:https://leetcode.com/problems/add-two-numbers/ 思路1:基本加法规则 根据小学学的基本加法 ...

  6. [Leetcode Week15] Add Two Numbers

    Add Two Numbers 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/add-two-numbers/description/ Descrip ...

  7. [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现

    [LeetCode] Add Two Numbers 两个数字相加   You are given two non-empty linked lists representing two non-ne ...

  8. [LeetCode] 2. 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

    Add Two Numbers 方法一: 考虑到有进位的问题,首先想到的思路是: 先分位求总和得到 totalsum,然后再将totalsum按位拆分转成链表: ListNode* addTwoNum ...

  10. LeetCode 2. add two numbers && 单链表

    add two numbers 看题一脸懵逼,看中文都很懵逼,链表怎么实现的,点了debug才看到一些代码 改一下,使本地可以跑起来 # Definition for singly-linked li ...

随机推荐

  1. python常用模块json、os、sys

    一.序列化 json & pickle 模块 json--用于字符串和Python数据类型间进行转换 pickle---用于python特有的类型和Python的数据类型间进行转换 json: ...

  2. 使用配置文件定义ADO.NET 的连接字符串

    最近一直在学习ADO.NET的相关知识,发现要对数据库操作的地方都要先创建一个连接字符串: string constr ="Data Source=(local);Initial Catal ...

  3. Difference between WCF and Web API and WCF REST and Web Service

    The .Net framework has a number of technologies that allow you to create HTTP services such as Web S ...

  4. tomcat -ROOT 与webapps 的关系,关于部署的一些问题

    现象:之前遇到很奇怪的问题,发完版之后没有效果,页面还是读取上一版的. 反复查找原因发现  http://localhost:8080/mobie 这个路径下的页面是正常的, 而  http://lo ...

  5. Torch7学习笔记(四)StochasticGradient

    使用随机梯度下降训练神经网络 StochasticGradient是一个比较高层次的类,它接受两个参数,module和criterion,前者是模型结构,后者是损失函数的类型.这个类本身有一些参数: ...

  6. perl学习之路3

    Perl编程之路3 标签: perl 列表与数组   Perl里面代表复数的就是列表和数组 列表(list)指的是标量的有序集合, 而数组(array)则是存储列表的变量. 在Perl这两个属于尝尝混 ...

  7. 安卓端网页浏览过程中实时更新title的web实现

    $(function () { var scrollTop = 0, //缓存上一次触发scroll的时候的scrollTop值 appendIndex = 0, //由于第23行append这个操作 ...

  8. 在C#中调用EXE文件

    1. 如果exe文件的返回值是int类型,标识操作执行的结果是否成功,例如: class Program { static int Main(string[] args) { return args. ...

  9. iOS 添加中文支持的操作

    1.选择工程菜单,这里要选中Project,而不是Targets   2.点击Info菜单, 下拉到最后,看到Localizations. 点击+号.   3.选择中文 chinese-simplif ...

  10. spring注解配置实例

    在spring中使用注解配置前需要先在配置文件指定需要扫描的包. 通过注解的方式依赖注入,可以不用创建set方法,也不用在xml文件中申明注入关系. 实例结构如下: 整个流程是: 先创建好数据库的表对 ...