[LEETCODE] #2 linked list add two numbers
阿新 • • 發佈:2018-12-18
https://leetcode.com/problems/add-two-numbers/
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.
* Definition:
/** https://leetcode.com/problems/add-two-numbers/ * @param {ListNode} l1 * @param {ListNode} l2 * @return {ListNode} */ function ListNode(val) { this.val = val; this.next = null; }
* Solution:
ListNode.prototype._tail = function() { for (var p = this; p.next != null; p = p.next) {} return p; }; ListNode.prototype.add = function(val) { var node = new ListNode(val); this._tail().next = node; return this; }; var deepClone = function(t, e) { "number" != typeof e && (e = 10); var i, n = e - 1; if (e > 0) if (t instanceof Date) i = new Date, i.setTime(t.getTime()); else if (t instanceof Array) { i = []; for (var o = 0, s = t.length; s > o; o++) i[o] = this.deepClone(t[o], n) } else if ("object" == typeof t) { i = {}; for (var r in t) if (t.hasOwnProperty(r)) { var a = t[r]; i[r] = this.deepClone(a, n) } } else i = t; else i = t; return i } ListNode.prototype.plus = function(/* ListNode */ al) /* :ListNode */ { var carry = 0, cur = 0, list = null; if (this == null) return deepClone(al); if (al == null) return deepClone(this); var p, q; for (p = this, q = al; p != null && q != null; p = p.next, q = q.next) { cur = p.val + q.val + carry; 9 < cur ? (cur -= 10, carry = 1) : (carry = 0); if (null === list) {list = new ListNode(cur);} else {list.add(cur);} } // console.log('carry='+carry); // console.log(p, q); for (; p != null; p = p.next) { cur = p.val + carry; 9 < cur ? (cur -= 10, carry = 1) : (carry = 0); list.add(cur); } for (; q != null; q = q.next) { cur = q.val + carry; 9 < cur ? (cur -= 10, carry = 1) : (carry = 0); list.add(cur); } if (carry > 0) { list.add(carry); } return list; }; ListNode.prototype.reverse = function() { if (this.next === null) { return this; } var head = this, prev = null, cur = head, next; while (cur !== null) { next = cur.next; // save next head = cur; // move cur to head cur.next = prev; prev = cur; // iterate cur = next; } return head; }; // main function var addTwoNumbers = function(l1, l2) { return l1.plus(l2); };
* test:
// test
ListNode.prototype.forEach = function(/* function */ c) {
for (var p = this, i = 0; p != null; p = p.next, ++i) {
c(p.val, i);
}
};
ListNode.prototype.toString = function() {
var a = [];
a.push('[');
this.forEach(function(v, i) {
a.push(v);
a.push(',');
});
a[a.length-1] = ']';
return a.join('');
};
function llist(/* Array */ a) {
var l = new ListNode(a[0]);
for (var i = 1; i < a.length; i++) {
l.add(a[i]);
}
return l;
}
// [5] + [5]
// var a1 = [9,8], a2 = [1];
// var a1 = [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], a2 = [5,6,4];
// var l1 = llist([2,4,3]), l2 = llist([5,6,4]);
// [6,6,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]
var a1 = [1], a2 = [9, 9];
var l1 = llist(a1), l2 = llist(a2);
console.log(addTwoNumbers(l1, l2).toString());