class ListNode {
val: number
next: ListNode | null
constructor(val?: number, next?: ListNode | null) {
this.val = (val===undefined ? 0 : val)
this.next = (next===undefined ? null : next)
}
}
function addTwoNumbers(l1: ListNode | null, l2: ListNode | null): ListNode | null {
let b = 0;
let start = null;
let temp = null;
while( l1 || l2){
let sum = 0;
sum = (l1?l1.val:0) + (l2?l2.val:0) + b
b = Math.floor(sum/10)
// console.log(sum%10)
if(!start){
start = temp = new ListNode(sum%10)
}else{
temp.next = new ListNode(sum%10)
temp = temp.next
}
l1 = l1?l1.next:null;
l2 = l2?l2.next:null;
if(b){
temp.next = new ListNode(b)
}
}
return start
};
// 342
const one = new ListNode(5);
// one.next = new ListNode(4);
// one.next.next = new ListNode(3);
// 465
const two = new ListNode(7);
// two.next = new ListNode(6);
// two.next.next = new ListNode(4);
console.log(addTwoNumbers(one,two));
console