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 ...
随机推荐
- .NET设计模式(4):建造者模式(Builder Pattern)
):建造者模式(Builder Pattern) .建造者模式的使用使得产品的内部表象可以独立的变化.使用建造者模式可以使客户端不必知道产品内部组成的细节. 2.每一个Builder都相对独立, ...
- JAVA 相关资料
在技术方面无论我们怎么学习,总感觉需要提升自已不知道自己处于什么水平了.但如果有清晰的指示图供参考还是非常不错的,这样我们清楚的知道我们大概处于那个阶段和水平. Java程序员 高级特性 反射.泛型. ...
- C# 执行批处理文件(*.bat)的方法代码
代码如下: static void Main(string[] args){ Process proc = null; try { st ...
- 关于通过bindService启动的service,在unbindService后service是否继续运行的讨论
有三种情况:如果直接使用服务,则没有必要进行绑定,但是如果要使用服务里面的方法,则要进行绑定.具体的启动情况有下: 1.当启动时,单独调用bindService方法,在unbindService后,会 ...
- 神经网络中的XOR问题
XOR问题 解决办法: 网络如图 其中激活函数 ReLU,令 即可解决XOR问题.
- php 微信3 自定义菜单
<pre name="code" class="php"><pre name="code" class="htm ...
- 读书笔记之 - javascript 设计模式 - 装饰者模式
本章讨论的是一种为对象增添特性的技术,它并不使用创建新子类这种手段. 装饰者模式可以透明地把对象包装在具有同样接口的另一对象之中,这样一来,你可以给一些方法添加一些行为,然后将方法调用传递给原始对象. ...
- html中子div用了浮动怎样让父div的大小自动撑开(清除浮动)
浮动子div撑开父div的几种方法: (1)在父div中在添加一个清除浮动的子div<div style=" clear:both;"></div>,该di ...
- DAC,MAC和SELinux,SEAndroid
1. 被ROOT了怎么办 2. SELinux 3. SEAndroid 4. JB(4.3) MR2的漏洞弥补 ------------------------------------------- ...
- PKM(personal knowledge management)
内化 一般含义 一般上,当涉及道德行为时,内化是巩固和植入某人信念.态度和价值的长期过程,而这一过程的实现牵扯到精神分析或行为方法的慎重使用. 当改变道德行为时,一组新的信念.态度和价值代替或适应于所 ...