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

代码:

 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. 在UEFI下安装windows和Ubuntu双系统目前不可行

    UEFI是BIOS的升级,未来将取代BIOS,说白了,就是跟BISO差不多的作用.但是目前比较新的主板兼容两种设置就比较坑了,默认是UEFI,UEFI下只能安装win8以上的版本,和linux64位系 ...

  2. Java UDP网络编程 - 最简单示例

    UDP也是网络通讯中的一个重要协议,与TCP区别可参见浅谈TCP/IP 和 UDP的区别,本文就对Java UDP通讯做一个简单例子介绍 服务端: package wyf; import java.i ...

  3. 如何让Form窗体接收KeyDown事件?

    在使用.Net Framework编写窗体应用程序的时候,有时有需要响应窗体的按键消息.当窗体上没有任何其他控件的时候,窗体是可以直接响应这些消息的. 但是当窗体上有其他控件时,会发现窗体再也不会响应 ...

  4. (easy)LeetCode 232.Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  5. @Valid springMVC bean校验不起作用

    @RequestMapping("/add2") public String addStudentValid(@Valid @ModelAttribute("s" ...

  6. Django: TemplateDoesNotExist at /admin/

    最近用virtualenv 总出现 Django: TemplateDoesNotExist at /admin/的问题,报错TemplateDoesNotExist at /admin/admin/ ...

  7. OC基础(4)

    NSString 类介绍及用法 结构体成员变量 对象和方法之间的关系 对象作为方法的参数连续传递 *:first-child { margin-top: 0 !important; } body &g ...

  8. Android的消息处理机制,handler,message,looper(一)

    当应用程序启动时,Android首先会开启一个主线程(也就是UI线程),主线程为管理界面中的UI控件.在程序开发时,对于比较耗时的操作,通常会为其开辟一个单独的线程来执行,以尽可能减少用户的等待时间. ...

  9. 基础字符串处理_C++

    C++中,有 char [ ] 和 string 两种方式处理字符串 char 数组是最原始的,string 是带迭代器的 正是这种 string 带了迭代器,它会使我们处理字符串很方便,但也十分慢 ...

  10. 关于NopCommerce3.6版的@Html.Widget(“home_page_top”)的说明

    以首页幻灯片为例子,首页幻灯片是在插件Nop.Plugin.Widgets.NivoSlider里面实现的 首页视图位置 这里其实是加载插件里面的视图内容,具体实现在插件实现 这个是扩展方法,就是执行 ...