LeetCode 445. Add Two Numbers II (两数相加 II)
题目标签:Linked List
题目给了我们两个 数字的linked list,让我们把它们相加,返回一个新的linked list。
因为题目要求不能 reverse,可以把 两个list 的数字都 存入 两个stack。利用了stack的特性,就可以从后面把数字相加,具体看code。
Java Solution:
Runtime: 6 ms, faster than 51.06%
Memory Usage: 45.4 MB, less than 64.71%
完成日期:07/08/2019
关键点:stack
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) { Stack<Integer> s1 = new Stack<Integer>();
Stack<Integer> s2 = new Stack<Integer>(); // store two numbers into stacks
while(l1 != null) {
s1.push(l1.val);
l1 = l1.next;
} while(l2 != null) {
s2.push(l2.val);
l2 = l2.next;
} // adding two numbers from stacks
int sum = 0;
ListNode list = new ListNode(0); while(!s1.empty() || !s2.empty()) {
if(!s1.empty())
sum += s1.pop();
if(!s2.empty())
sum += s2.pop(); list.val = sum % 10; // get the value
ListNode head = new ListNode(sum / 10); // get the carry
head.next = list;
list = head; sum /= 10;
} // need to check if the head is 0 or not
return list.val == 0 ? list.next : list;
}
}
参考资料:LeetCode Discuss
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/
LeetCode 445. Add Two Numbers II (两数相加 II)的更多相关文章
- 445 Add Two Numbers II 两数相加 II
给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个新的链表.你可以假设除了数字 0 之外,这两个数字都不会以零开头.进阶:如果输入链表 ...
- [leetcode]445. Add Two Numbers II 两数相加II
You are given two non-empty linked lists representing two non-negative integers. The most significan ...
- 2. Add Two Numbers[M]两数相加
题目 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...
- LeetCode 445. 两数相加 II(Add Two Numbers II)
445. 两数相加 II 445. Add Two Numbers II 题目描述 给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个 ...
- [LeetCode] 445. Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- Java实现 LeetCode 445 两数相加 II
445. 两数相加 II 给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会 ...
- [Swift]LeetCode445. 两数相加 II | Add Two Numbers II
You are given two non-empty linked lists representing two non-negative integers. The most significan ...
- LeetCode 445——两数相加 II
1. 题目 2. 解答 2.1 方法一 在 LeetCode 206--反转链表 和 LeetCode 2--两数相加 的基础上,先对两个链表进行反转,然后求出和后再进行反转即可. /** * Def ...
- Leetcode 445. 两数相加 II
1.题目描述 给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. ...
随机推荐
- Yii2的一些问题
Yii2中删除能不能串着用 Yii2中find.findAll有什么区别 Yii2中User::findOne($id)和User::find->where(['id'=>1])-> ...
- xStream.jar踩坑指南
前言 第一次接触Xstream,是在做一个socket通信的项目,由于是二次重新开发,所以有部分代码沿用了原来的代码(改造前用的webservice),其中xml字符串转换为对象,以及对象转换为xml ...
- JZOI1134 迷宫(maze)
#include <bits/stdc++.h> #define ll long long #define INF 2147483647 #define mem_INF 213906214 ...
- CSS:CSS 伪类(Pseudo-classes)
ylbtech-CSS:CSS 伪类(Pseudo-classes) 1.返回顶部 1. CSS 伪类(Pseudo-classes) CSS伪类是用来添加一些选择器的特殊效果. 语法 伪类的语法: ...
- python正则re
import reline = "Catsaresmarterthandogs"matchObj = re.match( r'(.*)are(\w{2})(.*)', line, ...
- Dynamic partition strict mode requires at least one static partition column.
https://blog.csdn.net/huobumingbai1234/article/details/81099856
- C++——数据结构之链表
直接上例子 int main() { ,,}; ,,}; Listnode *head=NULL,*temp; head=(Listnode*)malloc(sizeof(Listnode));//头 ...
- PAT_A1112#Stucked Keyboard
Source: PAT A1112 Stucked Keyboard (20 分) Description: On a broken keyboard, some of the keys are al ...
- PAT_A1095#Cars on Campus
Source: PAT A1095 Cars on Campus (30 分) Description: Zhejiang University has 8 campuses and a lot of ...
- element ui设置表格表头高度和每一行的高度
填坑记录:今天用element ui的表格组件做用户信息展示,直接拉取的官网的代码过来,发现表头和每一行都太高了,如下: 因为第一次使用element ui的表格组件,不太清楚会遇到这样的坑,以为能轻 ...