2. Add Two Numbers

官方的链接:2. Add Two Numbers

Description :

You are given two non-empty linked lists representing two non-negative integers. 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.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example:


Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.


问题描述

给定2个非空的代表非负整数的链表,数字是倒过来存储的,用链表表示求和。可以假设没有前导数字0。

思路

使用迭代解法,注意最后的进位。

[github-here]

 public class Q2_AddTwoNumbers {

     /**
* 迭代解法
*
* @param ListNode
* l1
* @param ListNode
* l2
* @return
*/
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if (null == l1 && null == l2) {
return new ListNode(0);
}
// 保存链表头,便于创建结点和最后返回结果
ListNode headNode = new ListNode(0);
ListNode sumNode = headNode;
// 和以及进位
int sum = 0;
int carry = 0;
while (null != l1 && null != l2) {
sum = l1.val + l2.val + carry;
sumNode.next = new ListNode(sum % 10);
carry = sum / 10;
sumNode = sumNode.next;
l1 = l1.next;
l2 = l2.next;
}
while (null != l1) {
sum = l1.val + carry;
sumNode.next = new ListNode(sum % 10);
carry = sum / 10;
sumNode = sumNode.next;
l1 = l1.next;
}
while (null != l2) {
sum = l2.val + carry;
sumNode.next = new ListNode(sum % 10);
carry = sum / 10;
sumNode = sumNode.next;
l2 = l2.next;
}
if (carry > 0) {
sumNode.next = new ListNode(carry);
sumNode = sumNode.next;
}
return headNode.next;
}
}

Q2:Add Two Numbers的更多相关文章

  1. LeetCode-2: Add Two Numbers

    [Problem:2-Add Two Numbers] You are given two non-empty linked lists representing two non-negative i ...

  2. LeetCode4:Add Two Numbers

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

  3. No.002:Add Two Numbers

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

  4. leetcode:Add Two Numbers

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

  5. LeetCode之“链表”:Add Two Numbers

    题目链接 题目要求: You are given two linked lists representing two non-negative numbers. The digits are stor ...

  6. LeetCode第[2]题(Java):Add Two Numbers (链表相加)——Medium

    题目难度:Medium 题目: You are given two non-empty linked lists representing two non-negative integers. The ...

  7. LeetCode OJ:Add Two Numbers (相加链表之数)

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

  8. 面试题:Add Two Numbers(模拟单链表)

    题干: You are given two non-empty linked lists representing two non-negative integers. The digits are ...

  9. LeetCode第二题:Add Two Numbers

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

随机推荐

  1. ThinkPhp3.2.3缓存漏洞复现以及修复建议

    小编作为一个php(拍黄片)的程序员,今天早上无意间看到thinkphp的缓存漏洞,小编在实际开发过程中用thinkphp3.2.3挺多的. 我们这里来复现一下漏洞 后面我会提出修复建议 首先我们下载 ...

  2. 剑指offer第12题打印从1到n位数以及大整数加法乘法

       字符和数字加减就是字符的ASCII码和数字直接加减. 方法一: 1)在字符串操作中给一个整形数字加(字符0)就是把它转化为字符,当然给一个字符减去(字符0)就可以把它转化为数字了:如果确实是最后 ...

  3. JSON数组序列化C#方法

    /// <summary> /// dataTable转换成Json格式 JSON对应关系 三层数组 /// </summary> /// <param name=&qu ...

  4. python_os 的知识点

    1. os.getcwd() #获得当前路径 2. os.listdir(path) #列出path路径下的所有目录名和文件名包括后缀 3. os.mkdir(path) #在path创建一个目录 4 ...

  5. 批量处理文件的Python程序

    经常批量处理文件,这里有个python的模板,保存一下 这个例子是把目录里面所有子目录的mp3文件放慢0.85倍并保存到./processed/目录下面. #coding=utf-8 import s ...

  6. arduino 通过串口接收string,int类型数据

    串口接收string类型数据源码如下 String comdata = ""; void setup() {     Serial.begin(9600); }   void lo ...

  7. Elasticsearch常用的设置

    action.destructive_requires_name: true     用于设置删除只限于特定名称指向的数据, 而不允许通过指定 _all来删除所有索引

  8. 《TensorFlow实战Google深度学习框架》笔记——TensorFlow环境搭建

    一.TensorFlow的主要依赖包 1.Protocol Buffer Protocol Buffer负责将结构化的数据序列化,并从序列化之后的数据流中还原出原来的结构化数据.TensorFlow中 ...

  9. 手动搭建简单的vue项目

    创建项目根目录 切换到根目录下 , 并执行 npm init , 所有选项都默认即可. 安装 webpack webpack-cli vue vue-loader 添加项目结构         

  10. SpringMVC: JSON

    SpringMVC:JSON讲解 什么是JSON? JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式,目前使用特别广泛. 采用完全独立于编 ...