๐Ÿ’ซ ETC/Leetcode

Week 1-2. [Linked List] 21. Merge Two Sorted Lists (Python3)

ming412 2023. 8. 29. 00:03

๋ฌธ์ œ ํ•ด๊ฒฐ ๊ณผ์ •

โœ๏ธ ๋‚˜๋งŒ์˜ ๋ฐฉ์‹์œผ๋กœ ๋ฌธ์ œ ์ •๋ฆฌํ•˜๊ธฐ (=์ฒด๊ณ„ํ™”)

https://leetcode.com/problems/merge-two-sorted-lists/

 

Merge Two Sorted Lists - LeetCode

Can you solve this real interview question? Merge Two Sorted Lists - You are given the heads of two sorted linked lists list1 and list2. Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists

leetcode.com

๋‘ ๊ฐœ์˜ ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, ์ด๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ Merge ํ•˜๋ผ.
  • ๊ฐ๊ฐ์˜ ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ๋Š” ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ๋˜์–ด ์žˆ๋‹ค.

 

๐Ÿ”Ž ์–ด๋–ค ์‹์œผ๋กœ ์ ‘๊ทผํ–ˆ๋Š”๊ฐ€?

list1๊ณผ list2 ๊ฐ๊ฐ์ด ์ด๋ฏธ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ ๋˜์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ๋‘˜์„ ๋น„๊ตํ•˜์—ฌ ๋” ํฐ ๊ฐ’์„ ์ฑ„ํƒํ•œ๋‹ค.  

 

๐Ÿ™ˆ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์„ ํƒ: ์ž…๋ ฅ์˜ ํฌ๊ธฐ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ

๋‘ ๊ฐœ์˜ ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ์˜ ์ตœ๋Œ€ ๊ธธ์ด๊ฐ€ 50์ด๋ฏ€๋กœ linear search๋ฅผ ์ด์šฉํ•  ๊ฒƒ์ด๋‹ค.

 

๐Ÿ“ ์˜์‚ฌ ์ฝ”๋“œ

 

๐ŸŽฏ ๊ตฌํ˜„ ์ฝ”๋“œ

์šฐ์„  ์•„๋ž˜ ์ฝ”๋“œ๋Š” ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ•œ๋‹ค.

์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ [1,2,3]๊ณผ [1,3,4]๋ฅผ ๋ณ‘ํ•ฉํ–ˆ์„ ๋•Œ [1,1,2,3,4,4]๊ฐ€ ๋‚˜์™€์•ผ ํ•˜๋Š”๋ฐ, [4,4,3,2,1,1]์ด ๋‚˜์˜จ๋‹ค.

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:

        if list1 == None and list2 == None: return None
        elif list1 == None: return list2
        elif list2 == None: return list1

        h1 = list1
        h2 = list2

        # initial
        head = None
        linked_lst = None
        if h1.val >= h2.val:
            head = ListNode(h2.val)
            h2 = h2.next
        else:
            head = ListNode(h1.val)
            h1 = h1.next
        linked_lst = head

        # ์ด์–ด๋ถ™์ด๊ธฐ
        while h1 != None and h2 != None :
            if h1.val >= h2.val:
                cur = ListNode(h2.val, linked_lst)
                h2 = h2.next
            else:
                cur = ListNode(h1.val, linked_lst)
                h1 = h1.next
            linked_lst = cur


        # ๋‚จ์€ ๊ฒƒ ์ด์–ด๋ถ™์ด๊ธฐ
        while h1 != None:
            cur = ListNode(h1.val, linked_lst)
            linked_lst = cur
            h1 = h1.next
        while h2 != None:
            cur = ListNode(h2.val, linked_lst)
            linked_lst = cur
            h2 = h2.next

        return linked_lst

 

๋‚ด ์ฝ”๋“œ๋ฅผ ํƒ๊ตฌํ•ด๋ณด๊ธฐ

โฑ ์„ฑ๋Šฅ ์ธก์ • (์‹œ๊ฐ„๋ณต์žก๋„/๊ณต๊ฐ„๋ณต์žก๋„)

์ฃผ์–ด์ง„ ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ•˜๋‚˜์”ฉ ์ˆœํšŒํ•˜๋ฏ€๋กœ O(N)์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐ–๋Š”๋‹ค.

 

โœ๏ธ ์„ฑ๋Šฅ ํšŒ๊ณ 

์„ฑ๋Šฅ์€ ๋‚˜์˜์ง€ ์•Š์ง€๋งŒ ์˜๋„์™€ ๋‹ค๋ฅด๊ฒŒ ๋™์ž‘ํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์ฝ”๋“œ๊ฐ€ ๋ณต์žกํ•˜๋‹ค.

 

๐Ÿ’ก ๋ฌด์—‡์„/์–ด๋–ป๊ฒŒ ๊ฐœ์„ ํ•  ์ˆ˜ ์žˆ์„๊นŒ?

cur = ListNode(h2.val, linked_lst) ์—์„œ h2.val์„ ๊ธฐ์กด linked_lst ๋’ค์— ๋ถ™์—ฌ์„œ ์˜ค๋ฆ„์ฐจ์ˆœ์ด ์•„๋‹Œ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ์ด ๋˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค.

์ด๋ฅผ ๊ฐœ์„ ํ•ด๋ณด์ž.

 

๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ์ฝ”๋“œ๋ฅผ ํƒ๊ตฌํ•ด๋ณด๊ธฐ

๐ŸŽฏ ๊ตฌํ˜„ ์ฝ”๋“œ

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:

        head = ListNode(-1) # dummy node # ์‹ค์ œ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ์˜ ์‹œ์ž‘ ๋ถ€๋ถ„์€ head.next์— ์œ„์น˜ํ•œ๋‹ค
        cur = head # ํ˜„์žฌ ์ƒ์„ฑ์ค‘์ธ ์ƒˆ๋กœ์šด ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์˜ ๋งˆ์ง€๋ง‰ ๋…ผ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

        while list1 != None and list2 != None:
            if list1.val <= list2.val:
                cur.next = list1
                list1 = list1.next
            else:
                cur.next = list2
                list2 = list2.next
            cur = cur.next # ๋‹ค์Œ ๋…ธ๋“œ๋กœ ์—…๋ฐ์ดํŠธ

        # ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ
        if list1 != None: cur.next = list1
        else: cur.next = list2

        return head.next

 

โฑ ์„ฑ๋Šฅ ์ธก์ • (์‹œ๊ฐ„๋ณต์žก๋„/๊ณต๊ฐ„๋ณต์žก๋„)

O(N)์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐ–๋Š”๋‹ค.

 

๋А๋‚€ ์ 

๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ง์ ‘ ๊ตฌํ˜„ํ•˜๋Š” ์—ฐ์Šต์„ ๋งŽ์ด ํ•ด๋ด์•ผํ•  ๊ฒƒ ๊ฐ™๋‹ค. ์•„์ง ๋‚ฏ์„ค๊ณ  ๋ถ€์กฑํ•จ์„ ๋А๊ผˆ๋‹ค.

 

์ฐธ๊ณ  ์ž๋ฃŒ

[Python ์œผ๋กœ ํ‘ธ๋Š” Leetcode]21. Merge Two Sorted Lists