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) {
int carry = 0;
ListNode newHead = new ListNode(0);
ListNode p1 = l1, p2 = l2, p3=newHead; while(p1 != null || p2 != null){
if(p1 != null){
carry += p1.val;
p1 = p1.next;
} if(p2 != null){
carry += p2.val;
p2 = p2.next;
} p3.next = new ListNode(carry%10);
p3 = p3.next; carry /= 10;
} if(carry == 1){
p3.next = new ListNode(1);
}
return newHead.next; }
}

Add Two Numbers LeetCode Java的更多相关文章

  1. LeetCode Two Add Two Numbers (JAVA)

    问题简介:输入两个数字链表,输出求和后的链表(链表由数字位数倒序组成) 问题详解: 给定两个非空链表,表示两个非负整数. 数字以相反的顺序存储,每个节点包含一位数字.对两个整数作求和运算,将结果倒序作 ...

  2. Add two numbers [LeetCode]

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

  3. Add Two Numbers ---- LeetCode 002

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

  4. Sum Root to Leaf Numbers leetcode java

    题目: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a nu ...

  5. LeetCode 面试:Add Two Numbers

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

  6. [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现

    [LeetCode] Add Two Numbers 两个数字相加   You are given two non-empty linked lists representing two non-ne ...

  7. leetcode 第二题Add Two Numbers java

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

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

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

  9. LeetCode Add Two Numbers II

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

随机推荐

  1. Metasploit爆破tcpwrapped服务

    转自:http://www.mamicode.com/info-detail-1653722.html 一.利用nmap工具扫描目标主机 1.1 使用nmap命令对目标主机进行扫描. 1.2 在终端中 ...

  2. gradle项目与maven项目相互转化

    gradle这几年发展迅猛,github越来越多的项目都开始采用gradle来构建了,但是并不是所有人都对gradle很熟悉,下面的方法可以把gradle转成maven项目,前提gradle项目目录结 ...

  3. 初试Scala解析XML

    使用Scala解析XML,充分体现了函数式编程的特点,简洁和明了.用Java去解析不是不行,只不过代码不够清晰明了. 首先先把XML文件读入到内存里: val someXml = XML.loadFi ...

  4. CCPC2016合肥现场赛

    A(hdu5961):(BFS) 题意:给两个有向图=P=(V,E​P​​)和Q=(V,E​Q​​), 满足1.E​P​​与E​Q​​没有交:2.E​P​​∪E​Q​​是竞赛图.判断P与Q是否同时为传 ...

  5. 51nod 1005 大数加法

    #include<iostream> #include<string> using namespace std; #define MAXN 10001 },b[MAXN]={} ...

  6. 解决 PHPExcel 长数字串显示为科学计数

    解决 PHPExcel 长数字串显示为科学计数 在excel中如果在一个默认的格中输入或复制超长数字字符串,它会显示为科学计算法,例如身份证号码,解决方法是把表格设置文本格式或在输入前加一个单引号. ...

  7. java中hashcode()和equals()的详解

    今天下午研究了半天hashcode()和equals()方法,终于有了一点点的明白,写下来与大家分享(zhaoxudong 2008.10.23晚21.36). 1. 首先equals()和hashc ...

  8. iOS常用 --- NSDictionary 与 NSMutableDictionary

    一.NSDictionary 字典的两种创建方法 NSDictionary *dic1 =[[NSDictionary alloc]init]; 2 // 或: 3 NSDictionary *dic ...

  9. .Net Core Linux centos7行—安装nginx,运行静态网站

    使用编译安装方式安装nginx Nginx下载地址:http://nginx.org/en/download.html.下载Stable version(稳定版就好).当前稳定版:http://ngi ...

  10. Python学习之day2

    1.执行Python脚本时打印的字符有颜色 print "\033[32;1mhello\033[0m" #打印绿色 print "\033[31;1mhello\033 ...