-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadd-two-numbers.js
110 lines (99 loc) · 2.72 KB
/
add-two-numbers.js
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* Source: https://leetcode.com/problems/add-two-numbers/
* Tags: [Linked List,Math]
* Level: Medium
* Title: Add Two Numbers
* Auther: @imcoddy
* Content: You are given two linked lists representing two non-negative numbers. 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.
*
* Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
* Output: 7 -> 0 -> 8
*/
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/
/**
* Memo: Iterate both list and add l2 to l1 until one is finished.
* Complex: O(n)
* Runtime: 288ms
* Tests: 1555 test cases passed
* Rank: S
* Updated: 2015-06-20
*/
var addTwoNumbers = function(l1, l2) {
var dummy = new ListNode(0);
dummy.next = l1;
var tail = dummy;
while (l1 && l2) {
l1.val = l1.val + l2.val + (tail.val >= 10 ? 1 : 0);
tail.val %= 10;
tail = l1;
l1 = l1.next;
l2 = l2.next;
}
if (!l1) tail.next = l2; //append to l2 if l2 is longer
while (tail && tail.val >= 10) {
tail.val %= 10;
if (tail.next) {
tail.next.val += 1;
tail = tail.next;
} else {
tail.next = new ListNode(1);
}
}
return dummy.next;
};
/**
* Memo: Iterate both list and add l2 to l1 until one is finished.
* Complex: O(max(m, n))
* Runtime: 288ms
* Tests: 1555 test cases passed
* Rank: S
* Updated: 2015-08-30
*/
var addTwoNumbers = function(l1, l2) {
var dummy = new ListNode(null);
dummy.next = l1;
var tail = dummy;
while (l1 && l2) {
l1.val = l1.val + l2.val;
tail = l1;
l1 = l1.next;
l2 = l2.next;
}
if (!l1) tail.next = l2; //append to l2 if l2 is longer
tail = dummy.next;
while (tail) {
if (tail.val > 9) {
tail.val %= 10;
if (tail.next) {
tail.next.val += 1;
} else {
tail.next = new ListNode(1);
}
}
tail = tail.next;
}
return dummy.next;
};
function ListNode(val) {
this.val = val;
this.next = null;
}
var util = require("./util.js");
var should = require('should');
console.time('Runtime');
util.lta(addTwoNumbers(util.atl([2, 4, 3]), util.atl([5, 6, 4]))).should.eql([7, 0, 8]);
util.lta(addTwoNumbers(util.atl([2, 4, 3]), util.atl([5, 6, 4, 1, 2]))).should.eql([7, 0, 8, 1, 2]);
(util.lta(addTwoNumbers(util.atl([1]), util.atl([9, 9, 9]))).should.eql([0, 0, 0, 1]));
(util.lta(addTwoNumbers(util.atl([1]), util.atl([9]))).should.eql([0, 1]));
console.timeEnd('Runtime');