第 7 天?
题目
给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。
例:
链表 1 -> 4 -> 3 & 2 -> 8 -> 2
输出 3 -> 2 -> 6
341 + 282 = 623
// 链表结构
function ListNode(val, next) {
this.val = val === undefined ? 0 : val;
this.next = next === undefined ? null : next;
}
思考
经过前面训练还用思考吗,直接冲
function addTwoNumbers(a, b) {
const res = new ListNode()
let temp = 0;
while (a || b || temp) {
const sum = a.val + b.val;
const [ten, bit] = sum.toString().padStart(2, '0')
a && a = a.next
b && b = b.next
res.val = bit + temp
}
}