Add Two Numbers

Total Accepted: 55216 Total Submissions: 249950My Submissions

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.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode newLink= new ListNode(0);
ListNode current=newLink;
int carry =0;
while (l1 != null || l2 != null) {
int a1 = (l1 == null) ? 0 : l1.val;
int a2 = (l2 == null) ? 0 : l2.val;
int sum = a1 + a2 + carry;
carry = sum / 10;
current.next = new ListNode(sum % 10);
l1 = (l1 == null) ? null : l1.next;
l2 = (l2 == null) ? null : l2.next;
current = current.next;
}
if (carry > 0) {
current.next = new ListNode(carry);
}
return newLink.next; }
}

leetcode2:Add Two Numbers的更多相关文章

  1. Leetcode-2 Add Two Numbers

    #2. Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits ...

  2. Leetcode2:Add Two Numbers@Python

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

  3. leetcode-2 Add Two Numbers 计算两个对应的列表和问题

     1.问题描写叙述: You are given two linked lists representing two non-negativenumbers. The digits are sto ...

  4. LeetCode-2. Add Two Numbers(链表实现数字相加)

    1.题目描述 You are given two non-empty linked lists representing two non-negative integers. The digits a ...

  5. Leetcode2.Add Two Numbers两数相加

    给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 -& ...

  6. LeetCode 2. 两数相加(Add Two Numbers)

    2. 两数相加 2. Add Two Numbers 题目描述 You are given two non-empty linked lists representing two non-negati ...

  7. 【LeetCode445】 Add Two Numbers II★★

    题目描述: 解题思路: 给定两个链表(代表两个非负数),数字的各位以正序存储,将两个代表数字的链表想加获得一个新的链表(代表两数之和). 如(7->2->4->3)(7243) + ...

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

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

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

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

随机推荐

  1. Python标准库10 多进程初步 (multiprocessing包)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 我们已经见过了使用subprocess包来创建子进程,但这个包有两个很大的局限性: ...

  2. springmvc对请求执行流程

    doService-->getHandlerMapping-->handlerMapping-->getHandler-->HandlerExecutionChain--> ...

  3. [Java] 内部类的用法

    package test.file; import java.io.File; import java.io.FilenameFilter; /** * 内部类的使用 * @author Frost. ...

  4. menu({postion:{my:"left top"},at:"right bottom"})里的my与at会冲突吗

    my(默认值:"center")类型:String描述:定义被定位元素上对准目标元素的位置:"horizontal vertical" 对齐方式.一个单一的值, ...

  5. esriSRGeoCS3Type Constants

    ArcGIS Developer Help  (Geometry)     esriSRGeoCS3Type Constants More available geographic coordinat ...

  6. ZOJ 3407 Doraemon's Cake Machine [数学]

    题意: 最多有2000组测试样例,每组样例代表n,m; n代表要把蛋糕平分的份数,m代表必须进行多少次操作. 一共有三种操作 1.竖切   经过蛋糕圆心,将蛋糕整个向下切. 2.横切   平行于蛋糕平 ...

  7. Linux下串口ttyS2,ttyS3不能用的问题解决办法

    PC104,Xlinux下,突然发现串口3,4不能用... 以为是硬件的问题,换成wince后,3,4工作正常,排除电路问题 在linux下查看dmesg: serial8250: ttyS0 at ...

  8. 双系统下恢复Ubuntu引导菜单

    引言 使用双系统的时候,我们经常重装Windows!那么有没有一种办法:只重装Windows而不重装Ubuntu呢? 在使用Win XP/Ubuntu双系统时,这个问题很好解决!但是在使用Win7(包 ...

  9. 浅谈Java的包装类

    一.什么是Java包装类 所谓Java包装类,就是将Java中的8种基本数据类型分别包装成为类的形式.包装类与基本数据类型的对应关系如下表所示. 基本数据类型 包装类 byte Byte short ...

  10. 【转】SQL注入(通过sqlmap来改变所有事情)

    第一步: sqlmap基于Python2.72版本,所以首先下载: https://www.python.org/  记住不要下载python3 第二步: 安装Python,将sqlmap解压到Pyt ...