JSFiddle - React, Tailwind, and code Playground
by krishna chaitanya nalluri
JavaScript
/**
* 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) {
if(!l1){
return l2;
}
if(!l2){
return l1;
}
const result = new ListNode(0);
let temp = result;
let carry = 0;
while( l1 || l2) {
const current = ((l1||{}).val || 0) + ((l2||{}).val || 0) + carry;
if(current > 9){
carry = 1;
temp.next = new ListNode(current - 10);
} else {
carry = 0;
temp.next = new ListNode(current);
}
l1 = (l1||{}).next;
l2 = (l2||{}).next;
temp = temp.next;
}
if(carry > 0){
temp.next = new ListNode(1);
}
return result.next;
};