* function ListNode(x){
* this.val = x;
* this.next = null;
* }
*/
/**
*
* @param head1 ListNode类
* @param head2 ListNode类
* @return ListNode类
*/
function ReverseList(pHead)
{
// write code here
if(!pHead || !pHead.next){
return pHead
}
let len = 0
let pre = null
let cur = null
while(pHead){
cur = pHead.next
pHead.next = pre
pre = pHead
pHead = cur
len++
}
return [pre,len]
}
function addInList( head1 , head2 ) {
// write code here
let [h1,len1] = ReverseList(head1)
let [h2,len2] = ReverseList(head2)
if(len2 > len1)
[h1,h2] = [h2,h1]
let carry = 0,num = 0
let resHead = h1
while(h2){
num = h1.val + h2.val + carry
h1.val = num % 10
carry = Math.floor(num/10)
h2 = h2.next
if(h2){
h1 = h1.next
}
}
while(carry){
if(h1.next){
h1 = h1.next
num = h1.val + carry
h1.val = num % 10
carry = Math.floor(num/10)
}else{
h1.next = new ListNode(1)
carry = 0
}
}
let [res,n] = ReverseList(resHead)
return res
}
module.exports = {
addInList : addInList
};