问题描述:

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. Directx3D SimpleSample Sample

    在d3d 2010 june这个版本里的samples 不知道为什么SimpleSample Sample 这个 它的documents基本等于没有 Starting point for new Di ...

  2. synergy--共享你的键鼠

    synergy用来分享一套键鼠,作用于多台主机.可作用于linux.Windows和mac平台.工作方式是:将一台主机作为服务器端,然后服务器端将会分享自己的键鼠,另一台主机作为客户端连接服务端就可以 ...

  3. iscsi 操作备忘

    #查找iscsi节点 iscsiadm -m discovery -t st -p 192.168.20.225 #列出可用节点 iscsiadm -m node -T iqn.com.exapmle ...

  4. DevOps 和技术债务偿还自动化

    当企业想要迁移到一个 DevOps 模型时,经常需要偿还高等级的技术债务 说得更明确一点,机构往往陷入「技术债务的恶性循环」中,以至于任何迅速.敏捷的迁移方式都无法使用.这是技术债务中的希腊债务危机水 ...

  5. ccflow学习下载网址

    1.ccflow下载:http://ccflow.org/download.aspx 2.说明:http://ccbpm.mydoc.io/ 3.各种文档:bbs.ccflow.org/showtop ...

  6. Java开发--操作MongoDB

    http://www.cnblogs.com/hoojo/archive/2011/06/01/2066426.html介绍到了在MongoDB的控制台完成MongoDB的数据操作,通过前一篇文章我们 ...

  7. C++:String类

    String类 1.使用String类必须在程序的开始包括头文件string,即要有如下语句:#include<string> 2.string类字符串对象的使用方法与其他对象一样stri ...

  8. (转)最新版的SSH框整合(Spring 3.1.1 + Struts 2.3.1.2 + Hibernate 4.1)

    最近一直有朋友在问,最新版的Spring.Struts.Hibernate整合老是有问题,昨晚大概看了一下.从Hibernate 4 开始,本身已经很好的实现了数据库事务模块,而Spring也把Hib ...

  9. OSCache 缓存技术

    前言:OSCache标记库由OpenSymphony设计,它是一种开创性的JSP定制标记应用,提供了在现有JSP页面之内实现快速内存缓冲的功能.OSCache是个一个广泛采用的高性能的J2EE缓存框架 ...

  10. PHP中该怎样防止SQL注入?

    因为用户的输入可能是这样的: ? 1 value'); DROP TABLE table;-- 那么SQL查询将变成如下: ? 1 INSERT INTO `table` (`column`) VAL ...