多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. Example: ~~~ Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 + 465 = 807. ~~~ ~~~ var addTwoNumbers = function(l1, l2) { const res = test(l1, l2, [], false); return res; }; var test = function (l1, l2, res, overflow = false) { res = res || []; l1 = l1 || { val: 0, next: null }; l2 = l2 || { val: 0, next: null }; const val = l1.val + l2.val + Number(overflow); if (val >= 10) { res.push(val % 10); overflow = true; } else { res.push(val); overflow = false; } if (l1.next || l2.next || overflow) { test(l1.next, l2.next, res, overflow); } return res; }; ~~~