问题描述:

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

解题思路:

设立一个头ListNode和尾ListNode,两个List分别从头开始,两两相加并且设立进位carry,如果相加超过10则carry为1,否则为零。同时需要考虑一个List还有长度,另一个已经结束的情况。

代码如下:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode head = new ListNode(0);
ListNode tail = head;
int sum = 0;
int carry = 0; while(l1 != null || l2 != null){
if(l1 == null){
sum = l2.val + carry;
l2 = l2.next;
}
else if (l2 == null){
sum = l1.val + carry;
l1 = l1.next;
}
else{
sum = l1.val + l2.val + carry;
l1 = l1.next;
l2 = l2.next;
} if(sum >= 10){
carry = sum / 10;
sum = sum % 10;
}
else{
carry = 0;
} tail.next = new ListNode(sum);
tail = tail.next;
} if(carry != 0){
tail.next = new ListNode(carry);
tail = tail.next;
} return head.next;
}
}

Java [leetcode 2] Add Two Numbers的更多相关文章

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

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

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

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

  3. LeetCode:1. Add Two Numbers

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

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

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

  5. LeetCode 面试:Add Two Numbers

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

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

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

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

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

  8. [Leetcode Week15] Add Two Numbers

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

  9. LeetCode之Add Two Numbers

    Add Two Numbers 方法一: 考虑到有进位的问题,首先想到的思路是: 先分位求总和得到 totalsum,然后再将totalsum按位拆分转成链表: ListNode* addTwoNum ...

随机推荐

  1. cf 363A B C

    A水题 ~~  注意0输出 /************************************************************************* > Author ...

  2. 理解lua 语言中的点、冒号与self

    转载自: http://blog.csdn.net/wangbin_jxust/article/details/12170233 lua编程中,经常遇到函数的定义和调用,有时候用点号调用,有时候用冒号 ...

  3. SGU 114

    114. Telecasting station time limit per test: 0.25 sec. memory limit per test: 4096 KB Every city in ...

  4. C# 实现:将一个文件夹下的.png图片全部移动到另一个文件夹

    如题,代码如下: using System; using System.IO; public class FileMove { public FileMove() { // TODO: } // co ...

  5. maven 命令备忘

    1. 打包时 不执行测试 mvn package -Dmaven.test.skip=true

  6. hdu2013

    http://acm.hdu.edu.cn/showproblem.php?pid=2013 #include<iostream> #include<stdio.h> #inc ...

  7. mac 设置 ll 等alias 并永久生效

    往上看了在.bash_profile中配置 然后 source  的方法, 试过了, 只是当前的终端有效,当电脑重启或者关闭终端就失效了,只好看看 mac 的 profile 代码 # System- ...

  8. SQLite操作(C# )

    C#连接SQLite的...方法 http://www.cnblogs.com/virusswb/archive/2010/09/17/SQLite1.html 1 SQLite简介 SQLite,是 ...

  9. 46. Permutations

    题目: Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the fo ...

  10. 开源入侵检测系统OSSEC搭建之一:服务端安装

    OSSEC是一款开源的多平台的入侵检测系统,可以运行于Windows, Linux, OpenBSD/FreeBSD, 以及 MacOS等操作系统中.主要功能有日志分析.完整性检查.rootkit检测 ...