https://leetcode.com/problems/add-two-numbers/

public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if(l1 == null) return l2;
if(l2 == null) return l1; ListNode head = new ListNode(0);
ListNode p = head; int tmp = 0;
while(l1!=null || l2!=null || tmp!=0) {
if(l1!=null){
tmp += l1.val;
l1 = l1.next;
}
if(l2!=null){
tmp += l2.val;
l2 = l2.next;
}
p.next = new ListNode(tmp%10);
p = p.next;
tmp = tmp/10;
}
return head.next;
}
}

[Leetcode] 002. Add Two Numbers的更多相关文章

  1. LeetCode #002# Add Two Numbers(js描述)

    索引 思路1:基本加法规则 思路2:移花接木法... 问题描述:https://leetcode.com/problems/add-two-numbers/ 思路1:基本加法规则 根据小学学的基本加法 ...

  2. 【JAVA、C++】LeetCode 002 Add Two Numbers

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

  3. LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters

    LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...

  4. LeetCode:1. Add Two Numbers

    题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...

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

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

  6. LeetCode 面试:Add Two Numbers

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

  7. [Leetcode Week15] Add Two Numbers

    Add Two Numbers 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/add-two-numbers/description/ Descrip ...

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

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

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

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

随机推荐

  1. JS工具函数汇总

    备注:http://phpjs.org/  这个站点把PHP常用的方法用js实现了,推荐一下 1.从数组中随机获取几个不重复项 //从一个给定的数组arr中,随机返回num个不重复项 function ...

  2. listen 54

    Our library is also open for the local residents. People are doing their Christmas shopping. Later t ...

  3. Linux下的磁盘缓存

    转自:http://blog.csdn.net/cywosp/article/details/21126161 前段时间在开发一个使用SSD做缓存的系统,在高速写入数据时会出现大量的磁盘缓存.太多的磁 ...

  4. oracle11g登录等问题

    一.oracle 11g登录服务开启 成功安装Oracle 11g后,共有7个服务,这七个服务的含义分别为:1. Oracle ORCL VSS Writer Service:Oracle卷映射拷贝写 ...

  5. C语言中的指针(一)

    指针也是一种数据类型,占用内存空间,内存中存储的只能是变量的地址. *p是操作内存的意思,在声明成为指针变量的时候使用*,在使用指针的时候,*表示操作内存. *p放在等号的左边,相当于是从内存中取值, ...

  6. CodeForces -163E :e-Government (AC自动机+DFS序+树状数组)

    The best programmers of Embezzland compete to develop a part of the project called "e-Governmen ...

  7. ntp服务器同步时间详细配置

    部署NTP服务器进行时间同步   NTP服务端:linl_S    IP:10.0.0.15 NTP客户端:lin_C    IP:10.0.0.16 NTP服务概述 1.原理 NTP(Network ...

  8. THUPC2019划水记

    虽然早就打不动了,虽然一个队友提前说好跑路了,还是两个人来玩了玩.最大的失误是没有开场打模拟题,然后就没骗到钱,还是要向某一心骗钱不顾排名的队伍学习.这次的模拟题超简单,很愉快地就打完了,也没调多久, ...

  9. BZOJ3123:[SDOI2013]森林

    浅谈主席树:https://www.cnblogs.com/AKMer/p/9956734.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem.p ...

  10. poj2356Find a multiple——鸽巢定理运用

    题目:http://poj.org/problem?id=2356 N个数,利用鸽巢定理可知应有N+1个前缀和(包括0),因此其%N的余数一定有重复: 同余的两个前缀和之差一定为N的倍数,据此得出答案 ...