-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy path2-add-two-numbers.js
More file actions
43 lines (43 loc) · 890 Bytes
/
2-add-two-numbers.js
File metadata and controls
43 lines (43 loc) · 890 Bytes
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
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/
var addTwoNumbers = function(l1, l2) {
let n1 = l1
let n2 = l2
let carry = 0
let res = null
let tail = res
while(n1 || n2) {
let num1 = n1 ? n1.val : 0
let num2 = n2 ? n2.val : 0
let sum = carry + num1 + num2
let nextNode = new ListNode(sum % 10)
if(tail === null) {
res = nextNode
} else {
tail.next = nextNode
}
tail = nextNode
carry = Math.floor(sum / 10)
if(n1) n1 = n1.next
if(n2) n2 = n2.next
}
if(carry>0) {
let nextNode = new ListNode(carry)
if(!tail) {
res = nextNode
} else {
tail.next = nextNode
}
}
return res
};