var TEST = function () { function _original(l1, l2) { let sum = NaN; let isAdd = false; let isNode1Exist = true; let isNode2Exist = true; let someOneIsNull = false; while (isNode1Exist || isNode2Exist) { isNode1Exist = isNode1Exist && !!l1; isNode2Exist = isNode2Exist && !!l2; sum = isAdd ? 1 : 0; sum += isNode1Exist ? l1.val : 0; sum += isNode2Exist ? l2.val : 0; isAdd = sum >= 10; if (isAdd) { sum-=10; } if (isNode1Exist) { l1.val = sum; if (someOneIsNull && !l1.next) { l1.next = isAdd ? new ListNode(1) : null; return true; } l1 = l1.next; someOneIsNull = someOneIsNull || !l1; } if (isNode2Exist) { l2.val = sum; if (someOneIsNull && !l2.next) { l2.next = isAdd ? new ListNode(1) : null; return false; } l2 = l2.next; someOneIsNull = someOneIsNull || !l2; } } } function _recursionAdd(rtnNode, l1, l2) { if (l1) { rtnNode.val += l1.val; l1 = l1.next; } if (l2) { rtnNode.val += l2.val; l2 = l2.next; } if (rtnNode.val >= 10) { rtnNode.val -= 10; rtnNode.next = _add(new ListNode(1), l1, l2); } else if (l1 && l2) { rtnNode.next = _add(new ListNode(0), l1, l2); } else { if (l1) { rtnNode.next = l1; } else if (l2) { rtnNode.next = l2; } } return rtnNode; } return { exec: function (l1, l2) { // ==== Recursion ==== /* return _original(l1, l2) ? l1 : l2; */ // ==== Recursion ==== /* return _recursionAdd(new ListNode(0), l1, l2); */ // ==== while loop ==== /* let rtnNode = new ListNode(0); let ptrNode = rtnNode; while (true) { if (l1) { ptrNode.val += l1.val; l1 = l1.next; } if (l2) { ptrNode.val += l2.val; l2 = l2.next; } if (ptrNode.val >= 10) { ptrNode.val -= 10; ptrNode.next = new ListNode(1); ptrNode = ptrNode.next; } else if (l1 && l2) { ptrNode.next = new ListNode(0); ptrNode = ptrNode.next; } else { if (l1) { ptrNode.next = l1; } else if (l2) { ptrNode.next = l2; } break; } } return rtnNode; */ } }; }();