Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var addTwoNumbers = function(l1, l2) {
  2.     let result = new ListNode(0);
  3.     let carryOver = 0;
  4.     let current = result;
  5.    
  6.     while (l1 !== null || l2 !== null || carryOver > 0) {
  7.         let sum = carryOver;
  8.        
  9.         if (l1 !== null) {
  10.             sum += l1.val;
  11.             l1 = l1.next;
  12.         };
  13.        
  14.         if (l2 !== null) {
  15.             sum += l2.val;
  16.             l2 = l2.next;
  17.         }
  18.        
  19.         carryOver = sum >= 10 ? 1 : 0;
  20.         current.next = new ListNode(sum % 10);
  21.         current = current.next;
  22.     }
  23.  
  24.     return result.next;
  25. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement