Skip to content

Instantly share code, notes, and snippets.

@heyogrady
Created May 18, 2023 20:37
Show Gist options
  • Select an option

  • Save heyogrady/e194bd145252e802902fa206a89dbc69 to your computer and use it in GitHub Desktop.

Select an option

Save heyogrady/e194bd145252e802902fa206a89dbc69 to your computer and use it in GitHub Desktop.
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/
//
var addTwoNumbers = function(l1, l2) {
let p1 = 0;
let p2 = 0;
var list = new ListNode();
var firstNode = list;
var carry = 0;
while(true) {
let total;
if(l1 === null) {
total = l2.val;
} else if(l2 === null) {
total = l1.val;
} else {
total = l1.val + l2.val; // 7
}
list.val = (total % 10) + carry;
console.log("list.val", list.val);
if(total > 9) {
carry = 1;
} else {
carry = 0;
}
list.next = new ListNode();
if(l1 !== null) { l1 = l1.next };
if(l2 !== null) { l2 = l2.next };
if(l1 === null && l2 === null) { break };
}
return firstNode;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment