1、题目描述

2、题目描述

题目思路可以参考合并单链表的思路,定义一个全局 进位标志,如果两个数值相加得到需要进位,则将进位标志置为1 。

3、代码

  ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {

         ListNode* head = new ListNode(  ) ;
ListNode* result = head;
ListNode* p1 = l1;
ListNode* p2 = l2;
int up = ;
while( p1 != NULL && p2 != NULL )
{
int r = p1->val + p2->val ;
r += up;
if( r < )
{
result->next = new ListNode( r ) ;
up = ;
}
else
{
result -> next = new ListNode( r -) ;
up = ;
}
p1 = p1->next;
p2 = p2->next;
result = result ->next;
} while( p1 != NULL )
{
if( p1->val + up < )
{
result->next = new ListNode( p1->val + up );
up = ;
}else{
result->next = new ListNode( p1->val + up - );
up = ;
}
p1 = p1->next;
result = result ->next;
} while( p2 != NULL )
{
if(p2->val + up < )
{
result->next = new ListNode( p2->val + up);
up = ;
}
else
{
result->next = new ListNode( p2->val + up - ) ;
up = ;
}
p2 = p2->next;
result = result->next;
} if( up != && p1 == NULL && p2 == NULL )
{
result->next = new ListNode(up) ;
}
return head->next; }

LeetCode题解之Add two numbers的更多相关文章

  1. 《LeetBook》LeetCode题解(2):Add Two Numbers [M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  2. leetcode题解2. Add Two Numbers

    题目: You are given two non-empty linked lists representing two non-negative integers. The digits are ...

  3. LeetCode题解 #2 Add Two Numbers

    题目大意:使用链表表示的两个整数,计算出其和,以同样的形式返回. Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 ...

  4. LeetCode 题解之Add Two Numbers II

    1.题目描述 2.分析 首先将链表翻转,然后做加法. 最后将结果链表翻转. 3.代码 ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { Lis ...

  5. leetcode 第二题Add Two Numbers java

    链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...

  6. 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...

  7. C# 写 LeetCode Medium #2 Add Two Numbers

    2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...

  8. LeetCode 第二题 Add Two Numbers 大整数加法 高精度加法 链表

    题意 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...

  9. leetcode@ [2/43] Add Two Numbers / Multiply Strings(大整数运算)

    https://leetcode.com/problems/multiply-strings/ Given two numbers represented as strings, return mul ...

随机推荐

  1. EF基础知识小记五(一对多、多对多处理)

    本文主要讲EF一对多关系和多对多关系的建立 一.模型设计器 1.一对多关系 右键设计器新增关联 导航属性和外键属性可修改 2.多对多关系 右键设计器新增关联 模型设计完毕之后,根据右键设计器根据模型生 ...

  2. Jmeter环境搭建

    一.工具描述 Apache JMeter是 100 %的 java 桌面应用程序.它可以被用来测试包括基于静态和动态资源程序的性能,例如静态文件, Java Servlets , Java  对象,数 ...

  3. 编写Android工程里测试代码的步骤

    第一步: 写个类去继承 AndroidTestCase public class TestStudent extends AndroidTestCase 并且编写一个测试的方法, 注意,测试的方法必须 ...

  4. Google Guava--Guava新增集合

    Multiset Multiset 虽然带了个set但是却允许重复元素,与set相同的是不保证元素顺序. 使用场景:获取文章中单词的出现次数 String[] wordArr = new String ...

  5. Chapter 3 Phenomenon——8

    I turned to sit up, and this time he let me, releasing his hold around my waist and sliding as far f ...

  6. java 集合框架小结

    一:集合框架  集合框架是为表示和操作集合而规定的一种统一的标准的体系结构.  任何集合框架都包含三大块内容:对外的接口.接口的实现和对集合运算的算法.   接口:即表示集合的抽象数据类型.Colle ...

  7. gitlab+jenkins+hook代码自动构建发布上线

    Gitlab+Jenkins+Hook 1.gitlab和jenkins的安装见: http://www.cnblogs.com/cuishuai/p/7544663.html http://www. ...

  8. SpringMVC 使用 RESTful 架构实现 CRUD 操作

    软件152 余建强 源码下载:http://download.csdn.net/detail/qq_35318576/9826210 1 使用框架 SpringMVC.Maven.Ajax.JSTL. ...

  9. Android应用博客目录

    应用有很多,开个博客都放进来方便查找,也方便修改 1 语言类: 1.1 JAVA 基础语言知识JAVA Collection与Collections,Array与Arrays的区别 JAVA练手--S ...

  10. 任务四十一:UI组件之日历组件(二)

    任务四十一:UI组件之日历组件(二) 面向人群: 有一定基础的同学 难度: 中 重要说明 百度前端技术学院的课程任务是由百度前端工程师专为对前端不同掌握程度的同学设计.我们尽力保证课程内容的质量以及学 ...