-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.js
59 lines (50 loc) · 1.29 KB
/
solution.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
/**
* 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 dummy = new ListNode(-1),
carry = 0,
stack1 = [],
stack2 = []
while (l1) {
stack1.push(l1)
l1 = l1.next
}
while (l2) {
stack2.push(l2)
l2 = l2.next
}
while (stack1.length && stack2.length) {
let node1 = stack1.pop(),
node2 = stack2.pop(),
temp = dummy.next,
val = (node1.val + node2.val + carry) % 10
carry = parseInt((node1.val + node2.val + carry) / 10)
dummy.next = new ListNode(val)
dummy.next.next = temp
}
let stack = stack1.length ? stack1 : stack2
while (stack.length) {
let node = stack.pop(),
temp = dummy.next,
val = (node.val + carry) % 10
carry = parseInt((node.val + carry) / 10)
dummy.next = new ListNode(val)
dummy.next.next = temp
}
if (carry) {
let temp = dummy.next
dummy.next = new ListNode(carry)
dummy.next.next = temp
}
return dummy.next
};