-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path002_add_two_nums.js
More file actions
47 lines (42 loc) · 936 Bytes
/
002_add_two_nums.js
File metadata and controls
47 lines (42 loc) · 936 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
44
45
46
47
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/
const addTwoNumbers = function(l1, l2) {
let l3 = new ListNode(0); // Default list val = 0, next = null
let sum = 0;
let p3 = l3;
while (l1 || l2) {
/**
* If sum > 10, need to get 1 because previous sum maybe 5 + 5 or 5+6
* @type {number}
*/
sum = Math.floor(sum /10);
if (l1) {
sum +=l1.val;
l1 = l1.next;
}
if (l2) {
sum +=l2.val;
l2 = l2.next;
}
l3.next = new ListNode(sum%10);
l3 = l3.next;
}
if (Math.floor(sum/10) === 1) {
l3.next = new ListNode(1);
}
return p3.next;
};
function ListNode(val) {
this.val = val;
this.next = null;
}