LeetCode 面试:Add Two Numbers
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的相加,数据结果不一定要用链表,也可以是数组,面试中可能两种都会问而且实现。
6 参考
LeetCode 面试:Add Two Numbers的更多相关文章
- LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters
LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...
- LeetCode:1. Add Two Numbers
题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...
- [LeetCode] 445. Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- LeetCode 面试:Add Binary
1 题目 Given two binary strings, return their sum (also a binary string). For example,a = "11&quo ...
- LeetCode #002# Add Two Numbers(js描述)
索引 思路1:基本加法规则 思路2:移花接木法... 问题描述:https://leetcode.com/problems/add-two-numbers/ 思路1:基本加法规则 根据小学学的基本加法 ...
- [Leetcode Week15] Add Two Numbers
Add Two Numbers 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/add-two-numbers/description/ Descrip ...
- [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现
[LeetCode] Add Two Numbers 两个数字相加 You are given two non-empty linked lists representing two non-ne ...
- [LeetCode] 2. Add Two Numbers 两个数字相加
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- LeetCode之Add Two Numbers
Add Two Numbers 方法一: 考虑到有进位的问题,首先想到的思路是: 先分位求总和得到 totalsum,然后再将totalsum按位拆分转成链表: ListNode* addTwoNum ...
随机推荐
- RedHat7搭建MongoDB集群
下载RPM安装包# wget -c -r -N -np -nd -L -nH https://repo.mongodb.org/yum/redhat/7/mongodb-org/stable/x86_ ...
- jquery插件select2事件不起作用(select2-3.5.4)
jquery插件select2事件不起作用 >>>>>>>>>>>>>>>>>>>&g ...
- Java基础--继承方法调用顺序
最近因为面试的原因,回过头来复习基础的知识,都忘光了,准备买本面试书回来啃. 我先把自己测试的结论总结写出来,以后忘记再来看看 如果b类继承自a类,在main方法中new出b的对象(不带参数),那么他 ...
- html语言中的meta元素
1.定义语言 格式:〈meta http-equiv=″Content-Type″ content=″text/html; charset=gb2312″〉 这是META最常见的用法,在制作网页时 ...
- Beyond Compare 设置打开文件的默认编码
转载:http://www.note4u.info/archives/360 Beyond Compare 每次打开都会以西欧(windows)打开文件,在有中文的地方,经常出现乱码.但是设置每个文件 ...
- 如何处理Tomcat日志catalina.out日志文件过大的问题
tomcat默认日志文件为catalina.out,随着系统运行时间的增加,该日志文件大小会不断增大,甚至增大到G级.不仅会导致我们无法使用常规工具查找系统问题,而且会影响tomcat性能(比如我在维 ...
- 3.SQL*Plus命令
3.1SQL*Plus与数据库的交互 主要用来数据库查询和数据处理的工具. 3.2SQL*Plus运行环境设置 3.2.1SET命令概述 用户可以使用SET命令设置SQL*Plus的运行环境,SET命 ...
- 一行代码实现headView弹簧拉伸效果
前言 很多app的个人中心上部的headView都实现了弹簧拉伸的效果,即tableView的top并不随着下拉而滑动,而是紧紧的停在屏幕的最上方. 我们今天就分析一下这个效果的实现方式. 分析 关键 ...
- jquery自适应布局
代码整理 - uix.layout.js /** * Grace [jQuery.js] * * UIX页面布局 * 290353142@qq.com * exp: * $.uix.layout(); ...
- java 反射取得方法入参类型的泛形
package TestReflectClass; import java.util.List; /** * Created by wangyang on 2016/12/16. */ public ...