1 Star 0 Fork 0

level/jslaobi-leetcode-js

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
0445.AddTwoNumbersII.js 716 Bytes
一键复制 编辑 原始数据 按行查看 历史
jslaobi 提交于 2020-12-18 02:29 . 445. 两数相加 II
var addTwoNumbers = function (l1, l2) {
const stack1 = [],
stack2 = [];
while (l1 !== null) {
stack1.push(l1.val);
l1 = l1.next;
}
while (l2 !== null) {
stack2.push(l2.val);
l2 = l2.next;
}
let curr = null,
carry = 0;
while (stack1.length !== 0 || stack2.length !== 0) {
let sum = 0;
if (stack1.length !== 0) {
sum += stack1.pop();
}
if (stack2.length !== 0) {
sum += stack2.pop();
}
sum += carry;
const node = new ListNode(sum % 10);
carry = Math.floor(sum / 10);
node.next = curr;
curr = node;
}
if (carry !== 0) {
const node = new ListNode(carry);
node.next = curr;
curr = node;
}
return curr;
};
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/keiferr/jslaobi-leetcode-js.git
[email protected]:keiferr/jslaobi-leetcode-js.git
keiferr
jslaobi-leetcode-js
jslaobi-leetcode-js
master

搜索帮助