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

代码:

 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. 20145305 《Java程序设计》第6周学习总结

    教材学习内容总结 1.输入串流代表对象为java.io.InputStream实例,输出串流代表对象为java.io.OutputStream实例 2.InputStream与OutputStream ...

  2. 百度之星IP聚合(水题map&字符处理)

    虽然题目停水的,但是好像字符处理运用的还比较合适 Problem Description 当今世界,网络已经无处不在了,小度熊由于犯了错误,当上了度度公司的网络管理员,他手上有大量的 IP列表,小度熊 ...

  3. php文件删除unlink()详解

    请记住从PHP文件创建的教训,我们创建了一个文件,名为testFile.txt . $myFile = "testFile.txt"; $fh = fopen($myFile, ' ...

  4. 《Code Complete》ch.11 变量名的力量

    What? 如何给变量命名 Why? 易读(你三个月前的代码=别人的代码),易记,恰如其分 整齐的命名具有美感,强迫症患者居家旅行杀人放火之必备 How? 以问题为导向 好名字反映的是问题(what) ...

  5. sqlserver 批量删除存储过程和批量修改存储过程的语句

    sqlserver 批量删除存储过程和批量修改存储过程的语句- sqlserver 批量删除存储过程和批量修改存储过程的语句,需要的朋友可以参考下. - 修改: 复制代码 代码如下: declare ...

  6. Android开发-API指南-<application>

    <application> 英文原文:http://developer.android.com/guide/topics/manifest/application-element.html ...

  7. vb 取得桌面路径

    txtPath.Text = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop)

  8. UIBlurEffect实现模糊效果

    //使用图片初始化背景 Pattern 图案,模式 self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageN ...

  9. ApplePay

    ApplePay要在项目有里配置,,配置好项目之后,就剩下编码了,做ApplePay首先要检查设备是否支持ApplePay,支持 ApplePay的设备在 iPhone6及以后,  PKPayment ...

  10. 在 MVC 控制器中使用 构造函数时行依赖注入 (IoC)

    在 Controller 中使用 构造函数进行依赖注入 (IoC) 1. Controller 代码: ICard card; ICardCategory cardCategory; public C ...