Skip to content

Instantly share code, notes, and snippets.

@lienista
Last active October 11, 2018 22:52
Show Gist options
  • Select an option

  • Save lienista/c176d01d591126d97f6bb28976a18bc4 to your computer and use it in GitHub Desktop.

Select an option

Save lienista/c176d01d591126d97f6bb28976a18bc4 to your computer and use it in GitHub Desktop.
Leetcode 2. Add Two Numbers - You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself.
const addTwoNumbers = (l1, l2) => {
if(l1 === null && l2 === null) {
return null;
}
if(l1 === null) return l2;
if(l2 === null) return l1;
let n1, n2, n, carry=0;
let l = new ListNode(); //our result
let pl = l; //pointer to run through l
while(l1 || l2){
n1 = l1 === null ? 0 : l1.val;
n2 = l2 === null ? 0 : l2.val;
n = n1 + n2 + carry;
carry = n>=10 ? 1 : 0;
n = n%10;
pl.val = n;
//we need to make sure that it's only added if it's not yet the end
if((l1 && l1.next) || (l2 && l2.next)) {
pl.next = new ListNode();
pl = pl.next;
}
if(l1 !== null) l1 = l1.next;
if(l2 !== null) l2 = l2.next;
}
if(carry > 0)
pl.next = new ListNode(carry);
return l;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment