1

/**
 * @param {string} s
 * @return {number}
 */
var lengthOfLongestSubstring = function(s) {

};
 
 
 
var addTwoNumbers = function(l1, l2) {
let a = l1;
let b = l2;
let he = 0;
let item = 1;
let jinOne = 0;
let newC = null;
let head = null;
let i =0;
while(a!=null||b!=null){
let tHe = 0;
if(a!=null)
tHe = a.val;
else
tHe = 0;
if(b!=null)
tHe = tHe+b.val;
else
tHe =tHe;

he =tHe;
if(he > 10) {
he = he % 10+jinOne;
jinOne = 1;
}else{
he = he+jinOne
jinOne = 0;
}
 
let newHe = new ListNode(he);
newHe.next=null;
if(i==0){
newC = newHe;
head = newC;
}else{
newC.next = newHe;
newC = newHe;
}
i++;
//console.log(he)
if(a!=null) a=a.next;
if(b!=null) b=b.next;
}

return head
};
 
//-------------
 
function ListNode(val) {
    this.val = val;
    this.next = null;
}
/*
let a1 = new ListNode(0);
    a2 = new ListNode(1);
//a3 = new ListNode(3);
let b1 = new ListNode(0);
   // b2 = new ListNode(6),
   // b3 = new ListNode(4);
a1.next=a2;a2.next=null;//a3;a3.next=null;
b1.next=null;//b2.next=b3;b3.next=null;    
*/
let  arr = '1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1'.split(',')
let brr = '5,6,4'.split(',')
let aHead =null;

function getList(str){
    let  myarr = '1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1'.split(',')
    let a =new ListNode(parseInt(arr[0]))
    for(let i=1;i<arr.length;i++){
        let newC = new ListNode(parseInt(arr[i]))
        newC.next = null;
        a.next = newC;
        a=newC;
    }
    a.next = null;
    return a;
}

function printL(a){
 while(a!=null){
    console.log(a.val)
    a=a.next
 }
}

let a1 = getList('1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1')
let b1 = getList('5,6,4')

printL(a1)



var addTwoNumbers = function(l1, l2) {
   let a = l1;
   let b = l2;
   let he = 0;
   let item = 1;
   while(a!=null||b!=null){
       let tHe = 0;
       if(a!=null) 
         tHe = a.val;
       else
          tHe = 0;
       if(b!=null)  
          tHe = tHe+b.val;
        else
          tHe =tHe;

       he = he+tHe*item;
       item = item *10;
       if(a!=null) a=a.next;
       if(b!=null) b=b.next;

   }
   console.log(he)
   let cItem = he%10
   let c = new ListNode(cItem)
   let head = c;
   while(he>=10){
        he =parseInt (he/10)
        cItem = he %10
        let newC = new ListNode(cItem)
        newC.next = null;
        c.next = newC;
        c = newC;
   }
   return head
 };

 let h = addTwoNumbers(a1,b1)

let a = h

 while(a!=null){
    console.log(a.val)
    a=a.next
 }
posted @ 2020-07-13 23:02  cnchengv  阅读(198)  评论(0编辑  收藏  举报