💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
link: https://leetcode.com/problems/add-two-numbers/description/ > 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. ``` # JavaScript > 小于32位的数字 ```js /** * Definition for singly-linked list. * function ListNode(val) { * this.val = val; * this.next = null; * } */ /** * @param {ListNode} l1 * @param {ListNode} l2 * @return {ListNode} */ var addTwoNumbers = function(l1, l2) { let arr1 = [] let arr2 = [] do { arr1.push(l1.val) l1 = l1.next } while (l1 !== null) do { arr2.push(l2.val) l2 = l2.next } while (l2 !== null) return String.prototype.split.call(parseInt(arr1.reverse().join('')) + parseInt(arr2.reverse().join('')), '').reverse().map( e => parseInt(e)) }; ``` > 如果是BigDecimal >[info] runtime: 209 ms ```js /** * Definition for singly-linked list. * function ListNode(val) { * this.val = val; * this.next = null; * } */ /** * @param {ListNode} l1 * @param {ListNode} l2 * @return {ListNode} */ var addTwoNumbers = function(l1, l2) { let a = [] let b = [] do { a.push(l1.val) l1 = l1.next } while (l1 !== null) do { b.push(l2.val) l2 = l2.next } while (l2 !== null) let figure = 0 let arr = [] let maxLen = a.length > b.length ? a.length : b.length let minLen = a.length < b.length ? a.length : b.length for (let i = 0; i < maxLen; i++) { if (i < minLen) { let sum = a[i] + b[i] + figure if (sum >= 10) { figure = 1 } else { figure = 0 } arr.push(sum % 10) } else { if (a.length > b.length) { sum = a[i] + figure if (sum >= 10) { figure = 1 } else { figure = 0 } arr.push(sum % 10) } else { sum = b[i] + figure if (sum >= 10) { figure = 1 } else { figure = 0 } arr.push(sum % 10) } } } if (figure === 1) { arr.push(figure) } return arr }; ``` >[info] runtime: 182 ms ```js var addTwoNumbers = function(l1, l2) { var nextNode = new ListNode(0); var head = nextNode; var carryover = 0; while(true){ if(l1 != null ){ nextNode.val += l1.val; l1 = l1.next; } if(l2 != null){ nextNode.val += l2.val; l2 = l2.next; } if(nextNode.val > 9){ var r = nextNode.val % 10; carryover = (nextNode.val - r) / 10; nextNode.val = r; } if(carryover == 0 && l1 == null && l2 == null){ return head; } nextNode.next = new ListNode(carryover); nextNode = nextNode.next; carryover = 0; } return head; }; ``` >[info] runtime: 212 ms ```js var addTwoNumbers = function(l1, l2) { var lw = []; var tmp, dtmp = 0; function add(l1v, l2v){ l1v = l1v || {val : 0}; l2v = l2v || {val : 0}; tmp = l1v.val + l2v.val + dtmp; dtmp = tmp % 10; lw.push(dtmp); if (tmp === dtmp){ dtmp = 0; }else{ dtmp = 1; } if(l1v.next || l2v.next || dtmp) add(l1v.next, l2v.next) } add(l1, l2) return lw; }; ``` >[info] runtime: 185 ms ```js var addTwoNumbers = function(l1, l2) { var head, current, list1, list2; var carry = 0, sum = 0, x = 0, y = 0; setup(); while (list1 != null || list2 != null) { addSumsToNode(); moveToNextNode(); } addNewNodeIfCarryExists(); return head.next; function setup() { head = new ListNode(0); current = head; list1 = l1 || new ListNode(0); list2 = l2 || new ListNode(0); } function addSumsToNode() { x = (list1 != null) ? list1.val : 0; y = (list2 != null) ? list2.val : 0; sum = carry + x + y; carry = (sum > 9) ? 1 : 0; current.next = new ListNode(sum % 10); } function moveToNextNode() { current = current.next; list1 = (list1 != null) ? list1.next : null; list2 = (list2 != null) ? list2.next : null; } function addNewNodeIfCarryExists() { if (carry > 0) { current.next = new ListNode(carry); } } }; ```