image


์˜ค๋žœ๋งŒ์— ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํฌ์ŠคํŒ…..! LeetCode์˜ Top Interview Questions๋ฅผ ํ’€๊ณ  ์žˆ๋‹ค. LeetCode๋Š” vscode์— ํ™•์žฅํŒฉ์ด ์žˆ์–ด์„œ ํ™ˆํŽ˜์ด์ง€์— ์•ˆ๋“ค์–ด๊ฐ€๊ณ  vscode์—์„œ ๋ฌธ์ œ ์ฝ๊ธฐ, ์ฝ”๋“œ ์ž‘์„ฑ, ํ…Œ์ŠคํŠธ ๋ฐ ์ž‘์„ฑ๊นŒ์ง€ ํ•œ ๋ฒˆ์— ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๊ฒŒ ์ง„์งœ ํŽธํ•ด์„œ LeetCode๋ฅผ ํฌ๊ธฐํ•  ์ˆ˜ ์—†๋Š”๋ฐ solved.ac ๋ฑƒ์ง€๊ฐ€ ํƒ๋‚˜์„œ ๋ฐฑ์ค€๋„ ํ’€๊นŒ ์ƒ๊ฐ ์ค‘์ด๋‹ค. ์šฐ์„  ๋‹น๋ถ„๊ฐ„์€ Top Interview Questions์„ ๊ณ„์†ํ•ด์„œ ํ’€ ๊ฒƒ ๊ฐ™๋‹ค.


๐Ÿ“Œ ๋ฌธ์ œ

https://leetcode.com/problems/add-two-numbers/


๐Ÿ“ ํ’€์ด

๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ ๋ฌธ์ œ๋Š” ๋ฆฟ์ฝ”๋“œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ช‡ ๋ฌธ์ œ ํ’€์–ด๋ณด๋‹ˆ๊นŒ ์œ ํ˜•์ด ์•„๋ž˜์™€ ๊ฐ™์ด ๋‚˜๋‰˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค.

1. ๊ธฐ์กด์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์กฐ์ž‘ํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ

๋ฆฌํ„ด์„ ์š”๊ตฌํ•˜์ง€ ์•Š๊ณ  ๊ธฐ์กด์˜ ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ๋ฅผ ์กฐ์ž‘ํ•˜๋ผ๊ณ  ํ•œ๋‹ค. ์ปค์„œ๋ฅผ ์š”๋ฆฌ์กฐ๋ฆฌ ์ž˜ ์˜ฎ๊ฒจ์„œ ํ’€๋ฉด ๋จโ€ฆ

2. ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ

๋‘ ๊ฐœ์˜ ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ฃผ์–ด์ง€๊ณ  ํ•ฉ์น˜๋ผ๋˜์ง€, ๊ต์ง‘ํ•ฉ์„ ๊ตฌํ•˜๋ผ๊ณ  ํ•˜๋ฏ€๋กœ ์ƒˆ๋กœ์šด ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค. ๋ณธ ๋ฌธ์ œ๋„ ์ด ์œ ํ˜•์— ํ•ด๋‹นํ•œ๋‹ค. ์ƒˆ๋กœ์šด ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๋งŒ๋“ค๋ฉด ๋œ๋‹ค. ์ฝ”๋“œ๋Š” 206.reverse-linked-list.py, 21.merge two sorted lists.py ์ฐธ๊ณ 

 newHead = ListNode(0)
 cur = newHead

 while ์กฐ๊ฑด๋ฌธ:
     # ...
     cur.next = ListNode(val)
     cur = cur.next

 return newHead.next # headNode์˜ ๊ฐ’์€ 0์ด๊ณ  next๋ถ€ํ„ฐ ๊ฐ’์„ ๋„ฃ์—ˆ์œผ๋ฏ€๋กœ next ๋ฐ˜ํ™˜

3. ๋ฆฌ์ŠคํŠธ๋ฅผ ๋น ๋ฅด๊ฒŒ ํƒ์ƒ‰ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ

๊ธธ์ด, ์ค‘๊ฐ„ ์œ„์น˜, ๋ณ‘ํ•ฉ ์ง€์ ์„ ๊ตฌํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ์ด๋‹ค. ๋‘ ๊ฐœ์˜ ํฌ์ธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. slow์™€ fast๋ผ๋Š” ๋‘ ๊ฐœ์˜ ๋Ÿฌ๋„ˆ ํฌ์ธํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด์„œ fast๋Š” ๋‘ ์นธ์”ฉ, slow๋Š” ํ•œ ์นธ์”ฉ ์ด๋™ํ•˜์—ฌ fast๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํƒ์ƒ‰์ด ๋๋‚ฌ์„ ๋•Œ slow๋Š” ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ€์šด๋ฐ์— ์žˆ์œผ๋ฉฐ, fast๋ฅผ ํ†ตํ•ด ๊ธธ์ด๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฝ”๋“œ๋Š” 234.Palindrome-linked-list ์ฐธ๊ณ 

์ด ๋ฌธ์ œ๋Š” 2๋ฒˆ์งธ ์œ ํ˜•์— ํ•ด๋‹นํ•œ๋‹ค. ๋‘ ๊ฐœ์˜ ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋™์‹œ์— ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•œ๋‹ค. divmod๋ฅผ ์‚ฌ์šฉํ•ด์„œ carry๋ฅผ ์ €์žฅํ•˜์˜€๋‹ค. ์ด ๋•Œ ํ•œ ๊ฐœ์˜ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋๊นŒ์ง€ ๋„๋‹ฌํ•˜๋ฉด ์ฒซ ๋ฒˆ์งธ while๋ฌธ์„ ๋๋‚ธ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์‚ผํ•ญ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•ด์„œ ๊ธธ์ด๊ฐ€ ๊ธด ๋ฆฌ์ŠคํŠธ๋ฅผ l4๊ฐ€ ๊ฐ€๋ฅดํ‚ค๊ฒŒ ํ•ด์„œ ํƒ์ƒ‰์„ ๊ณ„์†ํ•œ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ carry๊ฐ€ ๋‚จ์•„์žˆ๋Š” ๊ฒฝ์šฐ ์ƒˆ๋กœ์šด ์ž๋ฆฟ์ˆ˜๊ฐ€ ์ถ”๊ฐ€๋œ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— next์— 1์„ ๊ฐ€์ง„ ๋…ธ๋“œ๋ฅผ ๋ถ™์—ฌ์ค€๋‹ค. ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(max(m,n))

Top voted Solution์„ ๋ณด๋‹ˆ๊นŒ ํ•˜๋‚˜์˜ while๋ฌธ์—์„œ l1, l2์˜ val์„ ๊ณ„์† 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•˜๊ณ  not l1, l2์ธ ๊ฒฝ์šฐ val์„ ๋„ฃ์–ด์ฃผ์–ด์„œ ์ฒ˜๋ฆฌํ•ด์ฃผ๋Š” ์ฝ”๋“œ๋ฅผ ๋ดค๋‹ค. ํ•˜๋‚˜์˜ while๋ฌธ์—์„œ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์–ด์„œ ๋” ๊น”๋”ํ•œ ๊ฒƒ ๊ฐ™๋‹ค.


Python ์ฝ”๋“œ

class Solution(object):
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        l3 = ListNode()
        cur = l3
        carry = 0

        while l1 and l2:  # ๋‘˜ ์ค‘์— ํ•˜๋‚˜ ๋๊นŒ์ง€ ๊ฐ”์œผ๋ฉด
            carry, val = divmod(l1.val+l2.val+carry, 10)
            l1 = l1.next
            l2 = l2.next
            cur.next = ListNode(val)
            cur = cur.next

        l4 = l1 if l1 else l2  # ๋‚จ์•„์žˆ๋Š” ๋…ธ๋“œ ๊ณ„์† ๋”ํ•จ

        while l4:
            carry, val = divmod(l4.val+carry, 10)
            l4 = l4.next
            cur.next = ListNode(val)
            cur = cur.next

        if carry == 1:
            cur.next = ListNode(1)

        return l3.next