#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. 一行代码解决各种IE兼容问题,IE6,IE7,IE8,IE9,IE10

    行代码解决各种IE兼容问题,IE6,IE7,IE8,IE9,IE10 2012-04-25 16:29:04| 分类: 学习 |字号 订阅 在网站开发中不免因为各种兼容问题苦恼,针对兼容问题,其实IE ...

  2. 给IDEA设置单独的JDK

    一.系统参数设置: 1.64位IDEA:增加IDEA_JDK_64系统变量 2.32位IDEA:增加IDEA_JKD系统变量 如图: 二.参考说明 https://intellij-support.j ...

  3. Node.js-中文分词【1】-node-segment

    node-segment是基于盘古分词写的Node.js中文分词模块,鉴于盘古分词给我留下的好印象,我们在Node.js上选择了它 一.安装node-segment npm install -g se ...

  4. 浅谈CSS hack(浏览器兼容)

    今天简单写一点关于浏览器兼容的处理方法,虽然百度上已经有很多,但是我还是要写! 先看一个图 这个图描述了2016年1月至8月网民们所使用的浏览器市场份额(来源:http://tongji.baidu. ...

  5. jQuery页面加载初始化的3种方法

    jQuery 页面加载初始化的方法有3种 ,页面在加载的时候都会执行脚本,应该没什么区别,主要看习惯吧,本人觉得第二种方法最好,比较简洁. 第一种: $(document).ready(functio ...

  6. SOAPUI使用教程-REST请求工作

    双击一个REST请求在导航打开的REST请求编辑器窗口: 就像相应的SOAP请求编辑器,这个窗口有以下几部分组成: 工具栏在顶部有标准动作的和端口的下拉菜单轻松修改服务端口 请求编辑器左侧有相应编辑视 ...

  7. Python for Infomatics 第13章 网页服务三(译)

    注:文章原文为Dr. Charles Severance 的 <Python for Informatics>.文中代码用3.4版改写,并在本机测试通过. 13.6 应用程序接口API 现 ...

  8. IIS ISAPI

    cscript.exe %SYSTEMDRIVE%\inetpub\adminscripts\adsutil.vbs SET W3SVC/AppPools/Enable32bitAppOnWin64 ...

  9. PHP用户注册与登录【1】

    需求分析 主要功能分为 用户注册.用户登录.用户退出.用户中心 四个部分. 用户注册 用户注册主要功能有: 注册信息表单填写界面 javascript 脚本初步检测用户输入的注册信息. 注册处理模块检 ...

  10. XDocument获取指定节点

    string xmlFile = @"D:\Documents\Visual Studio 2013\Projects\Jesee.Web.Test\ConsoleApplication1\ ...