[Leetcode - Algorithm] Add two Numbers

hareguu·2021년 3월 7일
0

leetcode

목록 보기
3/3

Question


Answer

var addTwoNumbers = function (l1, l2) {
    let head = new ListNode(0);
    let node = head;
    let carry = 0;
    while (l1 || l2) {      
        let l1Value = l1? l1.val : 0;
        let l2Value = l2? l2.val : 0;
        let sum = l1Value + l2Value + carry;
        carry = 0;
        let newValue = sum;
        if (sum > 9) {
            newValue = sum % 10;
            carry = 1;
        }
        node.next = new ListNode(newValue);
        node = node.next;
        if (l1) {
            l1 = l1.next;
        }
        if (l2) {
            l2 = l2.next;
        }
    }
    if (carry) {
        node.next = new ListNode(carry);
    }
    return head.next;
};

실행 결과값

Runtime: 140 ms, faster than 53.41% of JavaScript online submissions for Add Two Numbers.
Memory Usage: 43.9 MB, less than 85.30% of JavaScript online submissions for Add Two Numbers.

profile
Who wanna be a programming nerd.

0개의 댓글