We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。 您可以假设除了数字 0 之外,这两个数都不会以 0 开头。 示例: 输入:(2 -> 4 -> 3) + (5 -> 6 -> 4) 输出:7 -> 0 -> 8 原因:342 + 465 = 807
遍历法
/** * 循环链表 */ var addTwoNumbers = function(l1, l2) { //#2 设置进位 let exceed = 0; let res = node = {}; while (l1 || l2) { //#2 计算总和 l1 + l2 + exceed let sum = (l1 && l1.val || 0) + (l2 && l2.val || 0) + (node.val || 0); //#3 取进位 exceed = parseInt(sum / 10); //#4 计算节点值 node.val = sum % 10; //#5 移动链表 l1 = l1 && l1.next; l2 = l2 && l2.next; //#6 设置下一个节点的值 if (l1 || l2 || exceed) { node.next = { val: exceed } } node = node.next; } return res; };
The text was updated successfully, but these errors were encountered:
No branches or pull requests
题目描述
算法
答案
The text was updated successfully, but these errors were encountered: