Skip to content

Commit 276ce10

Browse files
committed
21. Merge Two Sorted Lists
1 parent 7157d23 commit 276ce10

1 file changed

Lines changed: 110 additions & 0 deletions

File tree

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# from typing import Optional, List
2+
3+
# class ListNode:
4+
# def __init__(self, val=0, next=None):
5+
# self.val = val
6+
# self.next = next
7+
8+
# # ํŒŒ์ด์ฌ์˜ toString ์—ญํ• ์„ ํ•˜๋Š” __repr__์„ [1,3,4] ํ˜•ํƒœ๋กœ ์ถœ๋ ฅ๋˜๊ฒŒ ์ˆ˜์ •
9+
# def __repr__(self):
10+
# nodes = []
11+
# curr = self
12+
# while curr:
13+
# nodes.append(str(curr.val))
14+
# curr = curr.next
15+
# return "[" + ",".join(nodes) + "]"
16+
17+
# # ํŒŒ์ด์ฌ list๋ฅผ ListNode ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ๋Š” ํ—ฌํผ ํ•จ์ˆ˜
18+
# def make_linked_list(arr: List[int]) -> Optional[ListNode]:
19+
# if not arr:
20+
# return None
21+
# dummy = ListNode(0)
22+
# curr = dummy
23+
# for val in arr:
24+
# curr.next = ListNode(val)
25+
# curr = curr.next
26+
# return dummy.next
27+
28+
29+
# list1์˜ ๋…ธ๋“œ ๊ฐœ์ˆ˜๋ฅผ n, list2์˜ ๋…ธ๋“œ ๊ฐœ์ˆ˜๋ฅผ m์ด๋ผ ํ•  ๋•Œ
30+
# ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n + m)
31+
# ๊ณต๊ฐ„ ๋ณต์žก๋„: O(n + m) ->
32+
class Solution_01:
33+
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
34+
# ๋งจ ์•ž์— ๋”๋ฏธ ๋…ธ๋“œ๋ฅผ ํ•˜๋‚˜ ์ถ”๊ฐ€ํ•œ๋‹ค
35+
# ์—†๋Š” ์ƒํƒœ๋กœ ์งฐ๋Š”๋ฐ if node: else: ๊ตฌ๋ฌธ ๋Š˜์–ด๋‚˜์„œ ๋งจ ์•ž์— ๊ฐ€์ƒ์˜ ๋…ธ๋“œ๋ฅผ ํ•˜๋‚˜ ์ถ”๊ฐ€ํ•ด์คฌ๋‹ค
36+
dummy = ListNode(None)
37+
node = dummy
38+
39+
while True:
40+
# list1์ด ๋น„์–ด์žˆ์œผ๋ฉด ํ˜„์žฌ ๋…ธ๋“œ ๋’ค๋กœ list2๋ฅผ ์ด์–ด์ค€๋‹ค.
41+
if not list1:
42+
node.next = list2
43+
break
44+
45+
# list2๊ฐ€ ๋น„์–ด์žˆ์œผ๋ฉด ํ˜„์žฌ ๋…ธ๋“œ ๋’ค๋กœ list1์„ ์ด์–ด์ค€๋‹ค.
46+
if not list2:
47+
node.next = list1
48+
break
49+
50+
# ๊ฐ’์„ ๋น„๊ตํ•ด์„œ ์ž‘์€ ๊ฐ’์„ next ๋…ธ๋“œ์— ์ถ”๊ฐ€ํ•œ๋‹ค.
51+
if list1.val <= list2.val:
52+
node.next = ListNode(list1.val)
53+
# list1 ํ•˜๋‚˜ ์†Œ๋น„
54+
list1 = list1.next
55+
else:
56+
node.next = ListNode(list2.val)
57+
# list2 ํ•˜๋‚˜ ์†Œ๋น„
58+
list2 = list2.next
59+
60+
# ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ํ˜„์žฌ ๋…ธ๋“œ๋กœ ํ• ๋‹นํ•ด์ค€๋‹ค
61+
node = node.next
62+
63+
return dummy.next
64+
65+
66+
# [์ ‘๊ทผ๋ฒ•] Solution_01 ์˜ ๊ณต๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐœ์„ ํ–ˆ์Šต๋‹ˆ๋‹ค.
67+
# ListNode๋ฅผ ์ƒˆ๋กœ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  ๊ธฐ์กด ๋…ธ๋“œ๋ฅผ ํ™œ์šฉํ•˜๋„๋ก ์ˆ˜์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.
68+
69+
# list1์˜ ๋…ธ๋“œ ๊ฐœ์ˆ˜๋ฅผ n, list2์˜ ๋…ธ๋“œ ๊ฐœ์ˆ˜๋ฅผ m์ด๋ผ ํ•  ๋•Œ
70+
# ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n + m)
71+
# ๊ณต๊ฐ„ ๋ณต์žก๋„: O(1) -> dummy๋งŒ ์‚ฌ์šฉ, ์ถ”๊ฐ€ ๊ณต๊ฐ„ ๋ณต์žก๋„๋Š” O(1)
72+
class Solution_01:
73+
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
74+
# ๋งจ ์•ž์— ๋”๋ฏธ ๋…ธ๋“œ๋ฅผ ํ•˜๋‚˜ ์ถ”๊ฐ€ํ•œ๋‹ค
75+
# ์—†๋Š” ์ƒํƒœ๋กœ ์งฐ๋Š”๋ฐ if node: else: ๊ตฌ๋ฌธ ๋Š˜์–ด๋‚˜์„œ ๋งจ ์•ž์— ๊ฐ€์ƒ์˜ ๋…ธ๋“œ๋ฅผ ํ•˜๋‚˜ ์ถ”๊ฐ€ํ•ด์คฌ๋‹ค
76+
dummy = ListNode(None)
77+
node = dummy
78+
79+
while True:
80+
if not list1: # list1์ด ๋น„์–ด์žˆ์œผ๋ฉด ํ˜„์žฌ ๋…ธ๋“œ ๋’ค๋กœ list2๋ฅผ ์ด์–ด์ค€๋‹ค.
81+
node.next = list2
82+
break
83+
84+
if not list2: # list2๊ฐ€ ๋น„์–ด์žˆ์œผ๋ฉด ํ˜„์žฌ ๋…ธ๋“œ ๋’ค๋กœ list1์„ ์ด์–ด์ค€๋‹ค.
85+
node.next = list1
86+
break
87+
88+
# ๊ฐ’์„ ๋น„๊ตํ•ด์„œ ์ž‘์€ ๊ฐ’์„ next ๋…ธ๋“œ์— ์ถ”๊ฐ€ํ•œ๋‹ค.
89+
if list1.val <= list2.val:
90+
node.next = list1 # ์ƒˆ๋กœ์šด ListNode๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  list1๋ฅผ ํ• ๋‹นํ•ด์ค€๋‹ค.
91+
list1 = list1.next # list1 ํ•˜๋‚˜ ์†Œ๋น„
92+
else:
93+
node.next = list2 # ์ƒˆ๋กœ์šด ListNode๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  list2๋ฅผ ํ• ๋‹นํ•ด์ค€๋‹ค.
94+
list2 = list2.next # list2 ํ•˜๋‚˜ ์†Œ๋น„
95+
96+
# ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ํ˜„์žฌ ๋…ธ๋“œ๋กœ ํ• ๋‹นํ•ด์ค€๋‹ค
97+
node = node.next
98+
99+
return dummy.next
100+
101+
102+
# 4. ์˜ˆ์‹œ ์ž…๋ ฅ์œผ๋กœ ํ˜ธ์ถœ ๋ฐ ํ…Œ์ŠคํŠธ
103+
# if __name__ == "__main__":
104+
# list1 = make_linked_list([1, 2, 4])
105+
# list2 = make_linked_list([1, 3, 4])
106+
107+
# solution = Solution()
108+
# merged_list = solution.mergeTwoLists(list1, list2)
109+
110+
# print(merged_list)

0 commit comments

Comments
ย (0)