-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2-AddTwoNumbers.js
More file actions
33 lines (29 loc) · 938 Bytes
/
2-AddTwoNumbers.js
File metadata and controls
33 lines (29 loc) · 938 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
/**
* 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 result = new ListNode(0); // Dummy head
let cursor = result;
let carry = 0;
while (l1 || l2 || carry !== 0) {
let v1 = l1 !== null ? l1.val : 0;
let v2 = l2 !== null ? l2.val : 0;
let sum = v1 + v2 + carry;
//For JavaScript specifically, the Math.Floor is necessary to ensure that the var is truncated down to an integer
carry = Math.floor(sum / 10);
cursor.next = new ListNode(sum % 10);
cursor = cursor.next;
if (l1) l1 = l1.next;
if (l2) l2 = l2.next;
}
return result.next; // Return the next node to skip the dummy head
};