[LeetCode] 2. Add Two Numbers

서해빈·2021년 3월 2일
0

코딩테스트

목록 보기
3/65

문제 바로가기

Python

Iterative

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        answer = ListNode()
        answerNode = answer
        l1Node = l1
        l2Node = l2
        
        # first step
        sumVal = l1.val + l2.val
        l1Node, l2Node = l1.next, l2.next
        answerNode.val = sumVal % 10
        carry = int(sumVal / 10)

        # the others
        while l1Node or l2Node:
            sumVal = carry
            if l1Node:
                sumVal += l1Node.val
                l1Node = l1Node.next
            if l2Node:
                sumVal += l2Node.val
                l2Node = l2Node.next

            answerNode.next = ListNode(sumVal % 10)
            answerNode = answerNode.next
            carry = int(sumVal / 10)
        
        if carry:
            answerNode.next = ListNode(carry)
            answerNode = answerNode.next
            carry = 0

        return answer

Recursion

Linked List 구조이므로 회귀로 풀어봤다.

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def __init__(self, carry=0):
        self.carry = carry

    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        # 종료 조건
        if not (self.carry or l1 or l2):
            return None

        l1Next, l2Next = None, None
        sumVal = self.carry
        if l1:
            sumVal += l1.val
            l1Next = l1.next
        if l2:
            sumVal += l2.val
            l2Next = l2.next        
        
        self.carry = int(sumVal / 10)
        return ListNode(sumVal % 10, self.addTwoNumbers(l1Next, l2Next))
        

+ [2021.04.24 복습] Iterative

class Solution:
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        head = res = ListNode()
        over = 0
        while l1 or l2:
            sum_val = over + (l1.val if l1 else 0) + (l2.val if l2 else 0 )
            over = sum_val // 10
            head.next = ListNode(sum_val % 10)
            head = head.next
            if l1:
                l1 = l1.next
            if l2:
                l2 = l2.next
        if over:
            head.next = ListNode(over)
        
        return res.next

Javascript

Iterative

/**
 * 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) {
    const ansParent = new ListNode();
    let head = ansParent;
    let carry = 0;
    while (l1 || l2) {
        const val1 = l1 ? l1.val : 0;
        const val2 = l2 ? l2.val : 0;
        const sum = carry + val1 + val2;
        head.next = new ListNode(sum % 10);
        head = head.next;
        carry = parseInt(sum / 10, 10);
        l1 = l1 && l1.next;
        l2 = l2 && l2.next;
    }
    if (carry) {
        head.next = new ListNode(carry);
    }
    
    return ansParent.next;
};

Recursion

/**
 * 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, carry = false) {
    let node = null;
    if (l1 || l2) {
        const val1 = l1 ? l1.val : 0;
        const val2 = l2 ? l2.val : 0;
        const sum = val1 + val2 + carry;
        
        node = new ListNode(sum % 10);
        node.next = addTwoNumbers(l1 && l1.next, l2 && l2.next, sum >= 10)
    } else if (carry) {
        node = new ListNode(1);
    }
    
    return node;
};

0개의 댓글