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. XV6源代码阅读-文件系统

    Exercise1 源代码阅读 文件系统部分 buf.h fcntl.h stat.h fs.h file.h ide.c bio.c log.c fs.c file.c sysfile.c exec ...

  2. db.mysql.主从同步实验

    实验环境:windows10(1607).mysql5.7.16  (for windows zip) 主库(端口3306)配置文件: [mysqld] #数据库根目录 basedir = D:\my ...

  3. Git内部原理探索

    目录 前言 Git分区 .git版本库里的文件/目录是干什么的 Git是如何存储文件信息的 当我们执行git add.git commit时,Git背后做了什么 Git分支的本质是什么 HEAD引用 ...

  4. unity基础开发----Unity获取PC,Ios系统的mac地址等信息

    在软件开发中可以会用到mac地址作为,设备的唯一标示,我们也可以通过unity获取,经测试pc,ios都可以但是安卓没有获取到. 代码如下: using UnityEngine; using Syst ...

  5. DStream-04 Window函数的原理和源码

    DStream 中 window 函数有两种,一种是普通 WindowedDStream,另外一种是针对 window聚合 优化的 ReducedWindowedDStream. Demo objec ...

  6. Linux每日练习-批量删除用户,非脚本运行方式 20200225

  7. .NET配置问题

    Ext.NET MVC 配置问题总结       随着VS版本和.NET MVC版本.EF的版本的不断更新,虽然很多功能随着版本的提升而更完善,但对于旧版本开发的软件就有点悲催了,或许很多开发者都遇到 ...

  8. C++面试常见问题——06数组排序

    数组排序 冒泡.最简单的冒泡,没啥好讲的 #include<iostream> using namespace std; void BubbleSort(int a[],int len){ ...

  9. GDI+应用

    GDI+:Graphics Device Interface Plus也就是图形设备接口,提供了各种丰富的图形图像处理功能;在C#.NET中,使用GDI+处理二维(2D)的图形和图像,使用Direct ...

  10. Java8 新特性_Lambda 表达式

    1. Java8新特性_简介 Lambda 表达式 函数式接口 方法引用与构造器引用 Stream API 接口中的默认方法与静态方法 新时间日期 API 减少空指针异常的容器 Optional 2. ...