Problem solving/Algorithms

[LeetCode] Daily Challenge: Add Two Numbers (Python)

Young_A 2021. 1. 13. 08:59

LeetCode - Daily Challenge - Add Two Numbers

Problem Description

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 contains a single digit.

Add the two numbers and return the sum as a linked list.

 

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

 

Example 1 :

  • Input: l1 = [2,4,3], l2 = [5,6,4]
  • Output: [7,0,8]
  • Explanation: 342 + 465 = 807.

Example 2:

  • Input: l1 = [0], l2 = [0]
  • Output: [0]

Example 3:

  • Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
  • Output: [8,9,9,9,0,0,0,1]

 

Constraints:

  • The number of nodes in each linked list is in the range [1, 100].
  • 0 <= Node.val <= 9
  • It is guaranteed that the list represents a number that does not have leading zeros.

 

My Solution (Python)

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

class Solution(object):
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        result = ListNode()
        answer = result # head of result
        remainder = 0
        sum = 0
        while True:
            if l1 == None and l2 == None:
                if remainder == 1:
                    sum = 1
                    remainder = 0
                else:
                    break
            elif l1 == None and l2 != None:
                sum = l2.val + remainder
                l2 = l2.next
            elif l1 != None and l2 == None:
                sum = l1.val + remainder
                l1 = l1.next
            else:
                sum = l1.val + l2.val + remainder
                l1 = l1.next
                l2 = l2.next
            
            if (sum < 10):
                remainder = 0
            else:
                sum = sum - 10
                remainder = 1

            result.next = ListNode(sum)
            result = result.next
        return answer.next
  1. ๋ฆฌํ„ดํ•  ๋…ธ๋“œ์˜ ํ—ค๋“œ๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค. (์ด๋Š” ์ œ์ถœ์šฉ์„ ์œ„ํ•ด answer๋กœ ๋”ฐ๋กœ ์ €์žฅํ•ด์ฃผ์—ˆ์Œ.) 
  2.  ๋ฐ˜๋ณต๋ฌธ: ๋…ธ๋“œ๊ฐ€ Tail์ธ์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•œ if elif ... else ๋ฌธ์„ ์ด์šฉํ–ˆ๋‹ค.๊ฐ ๋…ธ๋“œ์˜ ๊ฐ’์ด sum์— ๋”ํ•˜๊ณ  ๋‚œ ๋’ค์—๋Š” ๋‹ค์Œ ๋…ธ๋“œ์˜ ๊ฐ’์„ ๊ธฐ์กด์˜ ๋ณ€์ˆ˜์— ์ €์žฅํ•œ๋‹ค.
    1. ๋‘ ๋…ธ๋“œ๊ฐ€ Tail์ผ๋•Œ, ๋‚จ์•„์žˆ๋Š” ๋‚˜๋จธ์ง€๋ฅผ ํ™•์ธํ•˜๊ณ  ๋‚˜๋จธ์ง€๊ฐ€ ์—†๋‹ค๋ฉด ๋ฐ˜๋ณต๋ฌธ์„ ์ข…๋ฃŒํ•œ๋‹ค.
    2. ๋‘ ๋…ธ๋“œ ์ค‘ ํ•˜๋‚˜๋งŒ Tail์ผ๋•Œ, Tail์ด ์•„๋‹Œ ๋…ธ๋“œ์˜ ๊ฐ’๊ณผ ๋‚˜๋จธ์ง€๋ฅผ ๋”ํ•œ sum ๊ฐ’์„ ๊ตฌํ•œ๋‹ค.
    3. ๋‘ ๋…ธ๋“œ ๋‹ค Tail์ด ์•„๋‹ˆ์ผ๋•Œ, ๋‘ ๋…ธ๋“œ์˜ ๊ฐ’๊ณผ ๋‚˜๋จธ์ง€๋ฅผ ๋”ํ•œ sum ๊ฐ’์„ ๊ตฌํ•œ๋‹ค.
  3.  ๊ฐ ๋…ธ๋“œ๋Š” 1 digit number๋งŒ ์ €์žฅํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ 10์ด ๋˜๋ฉด ์ผ์˜ ์ž๋ฆฌ ์ˆ˜๋งŒ ์ €์žฅํ•˜๊ณ  ๋‹ค์Œ ๋…ธ๋“œ์˜ ๊ฐ’์— 1์„ ์ถ”๊ฐ€ํ•˜๋ฏ€๋กœ if statement๋ฅผ ์ด์šฉํ•ด ํ™•์ธํ•ด์ค€๋‹ค.
    1. sum ๊ฐ’์ด 10 ๋ฏธ๋งŒ์ผ ๊ฒฝ์šฐ remainder๋Š” 0๊ฐ€ ๋œ๋‹ค.
    2. sum ๊ฐ’์ด 10 ์ด์ƒ์ผ ๊ฒฝ์šฐ sum ์—์„œ 10์„ ๋บ€ ๋’ค  ์ผ์˜ ์ž๋ฆฌ ๊ฐ’๋งŒ ์ €์žฅ, remainder์— 1์„ ์ €์žฅํ•œ๋‹ค.
  4. ๋ฐ˜๋ณต๋ฌธ์ด ์ข…๋ฃŒ๋˜๋ฉด Head์ธ answer์˜ ๋‹ค์Œ ๊ฐ’์ธ answer.next ๊ฐ’์„ ๋ฆฌํ„ดํ•œ๋‹ค. (answer.val ๊ฐ’์ด 0์ด๋ฏ€๋กœ)