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) {
if (l1 == null)
return l2;
if (l2 == null) //这题看起来简单,但是花了很长时间啊,链表要遍历几次,但是这样写代码最短?实在不想再写了
return l1;
int temp = 0;
ListNode p = l1;
ListNode q = l2;
ListNode longList = l1;
ListNode la = l1;
while (p != null && q != null) {
p = p.next;
q = q.next;
}
if (q == null) {
q = l1;
p = l2;
longList = l1;
} else {
q = l2;
p = l1;
longList = l2;
}
while (p != null && q != null) {
if (p.val + q.val + temp >= 10) {
q.val = p.val + q.val + temp - 10;
temp = 1;
} else {
q.val = q.val + p.val + temp;
temp = 0;
}
p = p.next;
q = q.next;
}
while (q != null) {
if (q.val + temp >= 10) {
q.val = q.val + temp - 10;
temp = 1;
} else {
q.val = q.val + temp;
temp = 0;
}
q = q.next;
}
if (temp == 1) {
ListNode last = new ListNode(1);
last.next = null;
p = longList;
while (p.next!= null)
p = p.next;
la = p;
la.next = last;
} return longList;
}
}

  

(LinkedList)2. Add Two Numbers的更多相关文章

  1. Add Two Numbers I & II

    Add Two Numbers I You have two numbers represented by a linked list, where each node contains a sing ...

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

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

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

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

  4. 52. 不用+、-、×、÷做加法[add two numbers without arithmetic]

    [本文链接] http://www.cnblogs.com/hellogiser/p/add-two-numbers-without-arithmetic.html [题目] 写一个函数,求两个整数的 ...

  5. Leetcode-2 Add Two Numbers

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

  6. [CareerCup] 2.5 Add Two Numbers 两个数字相加

    2.5 You have two numbers represented by a linked list, where each node contains a single digit. The ...

  7. [LintCode] Add Two Numbers 两个数字相加

    You have two numbers represented by a linked list, where each node contains a single digit. The digi ...

  8. LeetCode Add Two Numbers II

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

  9. [LeetCode_2] Add Two Numbers

    LeetCode: 2. Add Two Numbers /** * Definition for singly-linked list. * struct ListNode { * int val; ...

随机推荐

  1. python leetcode 日记--Maximal Square--221

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...

  2. 字符串中带有emoji表情处理

    1:先删除字符然后解析当前字符再显示 edit.addTextChangedListener(new TextWatcher() { @Override public void beforeTextC ...

  3. Android常见控件— — —ProgressBar

    ProgressBar用于在界面上显示一个进度条,表示我们的程序正在加载一些数据. <?xml version="1.0" encoding="utf-8" ...

  4. BLE Hacking:使用Ubertooth one扫描嗅探低功耗蓝牙

    0×00 前言 低功耗蓝牙(Low Energy; LE),又视为Bluetooth Smart或蓝牙核心规格4.0版本.其特点具备节能.便于采用,是蓝牙技术专为物联网(Internet of Thi ...

  5. magento jQuery冲突N种方法

    在做修改模板的时候在page中加入jquery库发现原本自带的js冲突 商品无法加入购物车,很多js都没有效果 这是jQuery和magento自带prototype的冲突解决版本有很多种,说个简单点 ...

  6. 使用Timer类的两个实例 动态时钟

    package chapter16; import javax.swing.*; import chapter15.StillClock; import java.awt.event.*; publi ...

  7. Map/Reduce 工作机制分析 --- 数据的流向分析

    前言 在MapReduce程序中,待处理的数据最开始是放在HDFS上的,这点无异议. 接下来,数据被会被送往一个个Map节点中去,这也无异议. 下面问题来了:数据在被Map节点处理完后,再何去何从呢? ...

  8. 第八课,T语言功能和参数(版本5.0)

    功能的理解 功能是TC移动项目应用的基本模块,通过对功能模块的调用实现特定的功能.TC综合开发工具中的功能相当于其它高级语言的子程序,在其他高级语言中,比如C,C++中,称为函数.允许用户建立自己定义 ...

  9. 第七课第四节,T语言流程语句(版本5.0)

    break语句 通常用在循环.遍历语句中.当跳出(break)语句用于循环语句中时,可使程序终止循环而执行循环后面的语句, 通常跳出 语句总是与如果语句联在一起.即满足条件时便跳出循环.可以说:跳出语 ...

  10. JAVA 编码中文简述

    中文编码问题虽然是个老问题,但对不熟悉的人来说还是不好处理的.不过Java中已经有了一套比较成熟的解决方案. 首先对中文编码格式予以简单介绍:中文编码有三套国标:GB2312,GBK,GB18030, ...