Week 1-2. [Linked List] 21. Merge Two Sorted Lists (Python3)
๋ฌธ์ ํด๊ฒฐ ๊ณผ์
โ๏ธ ๋๋ง์ ๋ฐฉ์์ผ๋ก ๋ฌธ์ ์ ๋ฆฌํ๊ธฐ (=์ฒด๊ณํ)
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)์ ์๊ฐ ๋ณต์ก๋๋ฅผ ๊ฐ๋๋ค.
๋๋ ์
๋งํฌ๋ ๋ฆฌ์คํธ๋ฅผ ์ง์ ๊ตฌํํ๋ ์ฐ์ต์ ๋ง์ด ํด๋ด์ผํ ๊ฒ ๊ฐ๋ค. ์์ง ๋ฏ์ค๊ณ ๋ถ์กฑํจ์ ๋๊ผ๋ค.