```
var addTwoNumbers = function(l1, l2) {
let head;
let result;
let carry = 0;
while (l1 || l2 || carry) {
let sum = 0;
let d1 = l1 ? l1.val : 0;
let d2 = l2 ? l2.val : 0;
sum = d1 + d2;
if (carry) {
sum = sum + carry;
carry = 0;
}
if (sum > 9) {
sum = sum % 10;
carry = 1;
}
if (!head) {
head = new ListNode(sum);
result = head;
} else {
result.next = new ListNode(sum);
result = result.next;
}
l1 = l1 ? l1.next : null;
l2 = l2 ? l2.next : null;
}
return head;
};
```