GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — JavaScript-Algorithm-Learning/src/leetcode/add-two-numbers.js at master · JackPu/JavaScript-Algorithm-Learning Expand file tree/ Copy pathadd-two-numbers.jsMore file actions 60 lines (55 loc) · 1.31 KB / Copy pathadd-two-numbers.jsFile metadata and controls60 lines (55 loc) · 1.31 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 // https://leetcode.com/problems/add-two-numbers/description/ /** * Definition for singly-linked list. * function ListNode(val) { * this.val = val; * this.next = null; * } */ /** * @param {ListNode} l1 * @param {ListNode} l2 * @return {ListNode} */ function ListNode(val) { this.val = val; this.next = null; } const addTwoNumbers = function (l1, l2) { const getVal = function (l1, l2, plus = 0) { const val1 = l1 && l1.val; const val2 = l2 && l2.val; const total = val1 + val2 + plus; return { val:total % 10, next: parseInt(total / 10), } } let l = null; let plus = 0; let i = 0; function setNode(vm, l1, l2) { if(!l1 && !l2 && !plus) { return vm; } const totalObj = getVal(l1, l2, plus) if (totalObj.next) { plus = totalObj.next; } else { plus = 0 } if(!l) { l = new ListNode(totalObj.val); vm = l; } else { vm.next = new ListNode(totalObj.val); vm = vm.next; } if( (l1 && l1.next) || (l2 && l2.next) || plus) { return setNode(vm, l1 && l1.next, l2 && l2.next) } } setNode(l, l1, l2) return l; }; module.exports = addTwoNumbers; |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |