题目:两个链表存储数字,然后求和,和值存储在一个链表中。

代码:

 public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode head = new ListNode(0);
ListNode result = head; int carry = 0 , tempSum = 0;
while(l1 != null || l2 != null){
int v1 , v2;
v1 = (l1 != null) ? l1.val : 0;
v2 = (l2 != null) ? l2.val : 0;
tempSum = v1 + v2 + carry;
carry = tempSum / 10;
tempSum = tempSum % 10;
head.next = new ListNode(tempSum);
head = head.next; l1 = (l1 != null) ? l1.next : null;
l2 = (l2 != null) ? l2.next : null;
} if(carry > 0) head.next = new ListNode(carry); return result.next;
}

[leetcode]_Add Two Numbers的更多相关文章

  1. LeetCode——Find All Numbers Disappeared in an Array

    LeetCode--Find All Numbers Disappeared in an Array Question Given an array of integers where 1 ≤ a[i ...

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

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

  3. [LeetCode] Valid Phone Numbers 验证电话号码

    Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...

  4. [LeetCode] Compare Version Numbers 版本比较

    Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 &l ...

  5. [LeetCode] Add Two Numbers 两个数字相加

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

  6. LeetCode Add Two Numbers II

    原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...

  7. LeetCode Compare Version Numbers

    原题链接在这里:https://leetcode.com/problems/compare-version-numbers/ 用string.split()方法把原有string 从小数点拆成 str ...

  8. LeetCode: Add Two Numbers 解题报告

    Add Two NumbersYou are given two linked lists representing two non-negative numbers. The digits are ...

  9. Leetcode:Add Two Numbers分析和实现

    Add Two Numbers这个问题的意思是,提供两条链表,每条链表表示一个十进制整数,其每一位对应链表的一个结点.比如345表示为链表5->4->3.而我们需要做的就是将两条链表代表的 ...

随机推荐

  1. linux下安装nginx、pcre、zlib、openssl

    1.安装nginx之前需要安装PCRE库的安装  最新下载地址   ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ tar –zxvf p ...

  2. .NET 请求被挂起,前端轮询,委托

    起因:因项目需要监控方法中计算进度,故而想通过AJAX调用,返回前端显示进度,结果开发中遇到第二个AJAX请求被挂起,需要等到第一个请求(计算)完成后,才会被处理到. 百度种种,知其原因是在Sessi ...

  3. [ActionScript 3.0] 自定义顶级类

    为了结合FlashBuilder编译参数,达到发布项目时不编译trace代码方便,写一个顶级类: package { public function tracing(...args):void { C ...

  4. [ActionScript 3.0] AS3虚线绘制方法

    import flash.geom.Point; import flash.display.MovieClip; import flash.display.Graphics; var mc:Movie ...

  5. MongoDB 多条件组合查询

    组合条件查询json格式语法 { "$and": [ { "Date": { $gt: ISODate("2015-06-05T00:45:00.00 ...

  6. WTL在Win8.1系统WM_DROPFILES无法响应的解决办法

    由于UAC的限制,WM_DROPFILES只能由权限较低的APP拖拽到权限较高的APP,反之如果从权限较高的APP拖拽到低权限的APP上,WM_DROPFILES不会被发送到低权限的APP消息队列.所 ...

  7. (转)c# math 计算两点之间的角度公式

    计算两点之间的角度公式是: 假设点一(X1,Y1),点二(X2,Y2) double angleOfLine = Math.Atan2((Y2 - Y1), (X2 - X2)) * 180 / Ma ...

  8. 安装LINUX X86-64的10201出现链接ins_ctx.mk错误

    在安装linux X86-64的Oracle10201时,在链接过程中出现了这个错误. 详细错误信息为: Error in invoking target ‘install’ of makefile  ...

  9. namespace用法

    1.在WCF.Controller中定义了一个UserModel,标记为① 2.在WCF.Controller.Model中定义了一个UserModel(同上,namespace不同),标记为② 3. ...

  10. PostMan入门使用教程

    最近需要测试产品中的REST API,无意中发现了PostMan这个chrome插件,把玩了一下,发现postman秉承了一贯以来google工具强大,易用的特质.独乐乐不如众乐乐,特此共享出来给大伙 ...