-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddTwoNumbers.js
54 lines (54 loc) · 1.09 KB
/
AddTwoNumbers.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
/**
* 2. Add Two Numbers
* 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 carry = 0;
let newList = new ListNode();
let curr = newList;
while (l1 && l2) {
curr.val = (carry + l1.val + l2.val) % 10;
if (carry + l1.val + l2.val > 9) carry = 1;
else carry = 0;
l1 = l1.next;
l2 = l2.next;
if (l1 || l2) {
curr.next = new ListNode();
curr = curr.next;
}
}
while (l1) {
curr.val = (carry + l1.val) % 10;
if (carry + l1.val > 9) carry = 1;
else carry = 0;
l1 = l1.next;
if (l1) {
curr.next = new ListNode();
curr = curr.next;
}
}
while (l2) {
curr.val = (carry + l2.val) % 10;
if (carry + l2.val > 9) carry = 1;
else carry = 0;
l2 = l2.next;
if (l2) {
curr.next = new ListNode();
curr = curr.next;
}
}
if (carry == 1) {
curr.next = new ListNode();
curr.next.val = 1;
}
return newList;
};