1 题目

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 addTwoNumbers(ListNode l1, ListNode l2);

2 思路

链表存储整数是低位前、高位后,逐位相加,注意进位。

复杂度

Time: O(n)

Space: O(n)  因为需要O(n)的空间来储存result。

3 代码

Definition for singly-linked list.

class ListNode {
int val;
ListNode next; ListNode(int x) {
val = x;
next = null;
}
}
     public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(0);
dummy.next = null;
ListNode tail = dummy;
for (ListNode p1 = l1, p2 = l2; p1 != null || p2 != null;) {
int a1 = p1 == null ? 0 : p1.val;
int a2 = p2 == null ? 0 : p2.val;
int sum = dummy.val + a1 + a2;
dummy.val = sum < 10 ? 0 : 1;
tail.next = new ListNode(sum % 10);
tail = tail.next;
p1 = p1 == null ? null : p1.next;
p2 = p2 == null ? null : p2.next;
}
if (dummy.val == 1)
tail.next = new ListNode(1);
return dummy.next;
}

4 总结

  • 利用头节点dummy来记录进位值,尾指针tail来构造result。
  • 代码考虑到了2个链表长度不相等的情况。
  • Similar as "Add Binary". The only difference is the pointer operation.

5 扩展

1.如果链表储存整数,不是逆序的,是高位前、地位后,如何解?

Input: (2 -> 4 -> 1) + (5 -> 7 -> 4)
Output: 8 -> 1 -> 5
想法:可以将input的2个链表反转,利用上面的解法求解,将结果链表在反转一次。
Time: O(n)  Space:O(1).

2.如果是BigInteger的相加,数据结果不一定要用链表,也可以是数组,面试中可能两种都会问而且实现。

3.然后接下来可以考一些OO设计的东西,比如说如果这是一个类应该怎么实现,也就是把数组或者链表作为成为成员变量,再把这些操作作为成员函数,进一步的问题可能是如何设计constructor,这个问题除了基本的还得对内置类型比如int, long的constructor, 类似于BigInteger(int num), BigInteger(int long). 总体来说问题还是比较简单,但是这种问题不能出错,所以还是要谨慎对待。

6 参考

  1. Add Two Numbers -- LeetCode
  2. LeetCode:Add Two Numbers
  3. Add Two Numbers leetcode java
  4. [LeetCode] Add Two Numbers, Solution

LeetCode 面试: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:1. Add Two Numbers

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

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

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

  4. LeetCode 面试:Add Binary

    1 题目 Given two binary strings, return their sum (also a binary string). For example,a = "11&quo ...

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

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

  6. [Leetcode Week15] Add Two Numbers

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

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

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

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

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

  9. LeetCode之Add Two Numbers

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

随机推荐

  1. Windows Server 2008 R2 域控制器部署指南

    一.域控制器安装步骤: 1.装 Windows Server 2008 R2并配置计算机名称和IP地址(见 附录一) 2.点击“开始”,在“搜索程序和文件”中输入Dcpromo.exe后按回车键: 3 ...

  2. Eclipse - 安装 run-jetty-run 插件及使用 jrebel 热部署

    安装 run-jetty-run 插件 1. 下载 run-jetty-run 2. 解压至 Eclipse/MyEclipse 安装目录下的 plugin 3. 右键 web 项工程,选择 Run ...

  3. “jni.h”: No such file or directory

    VS2010解决方案: 进入 “包含目录“ 方式: 右键项目属性页-> 配置属性->VC++目录->包含目录 在”包含目录“中编辑 添加以下路径: C:\Program Files\ ...

  4. yieId浅谈

    例子:在不使用yieId时,通常我们都会采取先遍历再把元素加到新的List中 using (var reader = SqlHelper.ExecuteReader("")) { ...

  5. CSS 伪元素

    CSS伪元素是用来添加一些选择器的特殊效果. 语法 伪元素的语法: selector:pseudo-element {property:value;} CSS类也可以使用伪元素: selector.c ...

  6. repeater控件实现分页

    repeater控件实现排序的方法,今天我再向大家介绍repeater控件如何实现分页的效果. 分页分为真分页和假分页. 真分页:控件上一页需要显示多少数据,就从数据库取出并绑定多少数据,每次换页时都 ...

  7. OnClose()和 OnDestroy()

    OnClose()和 OnDestroy() 基于对话框的MFC程序,发现每次程序退出时,托盘的小图标不能自动消失,鼠标移上去之后才能消失,比较不爽. 后来发现我删除这个图标的代码是在自己重写的OnC ...

  8. ASP.NET 后台不识别ASPX中的控件

    请问后台不识别ASPX中的控件,怎么解决 这个程序是在网上下载的 C# code <asp:DataGrid runat="server" ID="dgList1& ...

  9. directUI

    MFC界面开发中,习惯了使用控件,亦或者是自绘制控件来美化界面,但操作起来繁琐,还不太美观.DirectUI的出现,对于界面开发,给了我们一个新的选择,目前很多公司使用了该技术对其产品进行了美化,效果 ...

  10. C语言学习总结(四) 剩余内容

    第六章.剩余内容 (预处理指令,宏定义,条件编译,文件操作) 预处理指令 简单的来说就是在程序编译之前需要做的事情 1.宏定义 概念: 是一个替换代码的预处理指令,可以在编译之前进行代码替换(宏展开, ...