#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. C语言题目复习前7章重点程序

    /** #include <stdio.h> #include <stdlib.h> int max(int n1, int n2) { return (n1 > n2) ...

  2. Code First 关系配置整理

    之前EF一直有性能问题以及使用便利性问题, 终于到了EF6有了Migrations之后, 小弟也决定加入EF阵营了. 在学习FluentAPI配置关系的时候, 发现网上的好几个教程实际上博主自己都没有 ...

  3. 全本软件白名单 Quanben Software Whitelist

    Windows应用软件 Windows Applications (TBU) 全本推荐微软Windows 10操作系统 Quanben recommends Microsoft Windows 10 ...

  4. 德国W家HIPP 奶粉有货播报:2014.6.25 HIPP 1+ 4盒装有货啦!

    德国W家HIPP 奶粉有货播报:2014.6.25 HIPP 1+ 4盒装有货啦!

  5. Web API Get Started First

    注:此博客是自官网修剪而来,博主IT新手 一.web api与web service的不同: web api是基于Http协议,而web service是基于soap协议.两协议的区别小子看了很多,但 ...

  6. Django 权限管理

    对于Django而言,虽然自带了一些基本的通用权限限制,但现实中,可能我们更希望自己去定义业务权限划分 Django对于权限这块的部分验证方法 user = request.user user.is_ ...

  7. js判断浏览器是否为IE浏览器

    if (!!window.ActiveXObject || "ActiveXObject" in window) {//判断是否IE浏览器 } MSIE这样关键字之类的判断,IE1 ...

  8. JAVA基础整理-集合篇(一)

    集合作为JAVA的基础知识,本来感觉自己理解的很清楚了,但是在最近的一次面试中还是答得不尽如人意!再次做一下整理,以便加深理解以及随时查阅. 首先,java.util包中三个重要的接口及特点:List ...

  9. gkENGINE重开!

    2013年中,曾信誓旦旦的要开源gkENGINE,结果一直到了现在. 拖了一年多,问题在于 - 工作太忙... 其实在2014春节假期我还是赶了赶进度,对gles2的渲染器进行了完善,但没做完.然后留 ...

  10. js,css控制网页内容不让选中和复制

    ---恢复内容开始--- JS, CSS控制网页内容不让选择和复制 CSS 控制: <style> body{ -moz-user-select:none;//针对火狐浏览器,谷歌则-we ...