工具:vscode
语言:JavaScript
没按照题目给的链表来做,按照数组的方法做的
var addTwoNumbers = function (l1, l2) {
let resultList = [];
if (l1.length > l2.length) {
for (i = 0; l2.length < l1.length; i++){
l2.push(0)
}
} else if (l1.length < l2.length){
for (i = 0; l1.length < l2.length; i++){
l1.push(0)
}
}
for (i = 0; i < l1.length; i++) {
resultList[i] = l1[i] + l2[i];
}
for (i = 0; i < resultList.length; i++){
if (resultList[i] >= 10) {
if (resultList[i + 1] === undefined) {
resultList[i + 1] = 0;
}
const a = Math.floor(resultList[i] / 10)
resultList[i] = resultList[i] % 10;
resultList[i + 1] = resultList[i + 1] + a;
}
}
console.log(resultList);
};