File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed
Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change 1+ '''
2+ ๋ฌธ์ : ๋ ๊ฐ์ ์ ๋ ฌ๋ ์ฐ๊ฒฐ ๋ฆฌ์คํธ๋ฅผ ๋ณํฉํ์ฌ ํ๋์ ์ ๋ ฌ๋ ์ฐ๊ฒฐ ๋ฆฌ์คํธ๋ฅผ ๋ง๋์์ค.
3+ ํ์ด: ๋ ์ฐ๊ฒฐ ๋ฆฌ์คํธ์ ๋
ธ๋๋ฅผ ๋น๊ตํ๋ฉด์ ์์ ๊ฐ์ ๊ฐ์ง ๋
ธ๋๋ฅผ ๊ฒฐ๊ณผ ๋ฆฌ์คํธ์ ์ถ๊ฐํ๋ ๋ฐฉ์์ผ๋ก ๋ณํฉํฉ๋๋ค.
4+ ์๊ฐ ๋ณต์ก๋: O(n + m), n๊ณผ m์ ๊ฐ๊ฐ ๋ ์ฐ๊ฒฐ ๋ฆฌ์คํธ์ ๊ธธ์ด์
๋๋ค. ๋ ๋ฆฌ์คํธ์ ๋ชจ๋ ๋
ธ๋๋ฅผ ํ ๋ฒ์ฉ ๋ฐฉ๋ฌธํ๋ฏ๋ก ์ ์ฒด ์๊ฐ ๋ณต์ก๋๋ O(n + m)์
๋๋ค.
5+ ๊ณต๊ฐ ๋ณต์ก๋: O(1), ์ถ๊ฐ์ ์ธ ์ฐ๊ฒฐ ๋ฆฌ์คํธ๋ฅผ ์์ฑํ์ง ์๊ณ ๊ธฐ์กด ๋
ธ๋๋ค์ ์ฌ์ฌ์ฉํ๋ฏ๋ก ๊ณต๊ฐ ๋ณต์ก๋๋ O(1)์
๋๋ค.
6+ ์ฌ์ฉํ ์๋ฃ๊ตฌ์กฐ: ์ฐ๊ฒฐ ๋ฆฌ์คํธ
7+ '''
8+
9+
10+
11+ # Definition for singly-linked list.
12+ # class ListNode:
13+ # def __init__(self, val=0, next=None):
14+ # self.val = val
15+ # self.next = next
16+ class Solution :
17+ def mergeTwoLists (self , list1 : Optional [ListNode ], list2 : Optional [ListNode ]) -> Optional [ListNode ]:
18+ if list2 == None :
19+ return list1
20+ if list1 == None :
21+ return list2
22+
23+ if list1 .val <= list2 .val :
24+ a , b = list1 , list2
25+ else :
26+ b , a = list1 , list2
27+ # ์์ ๋ณต์ฌ ํ์ฉ (์ค์ํด๋ณด์)
28+ result = a
29+ while a != None and b != None :
30+ if a .next != None and a .val <= b .val and a .next .val <= b .val :
31+ a = a .next
32+ else :
33+ a .next , b = b , a .next
34+ a = a .next
35+ return result
36+
37+
You canโt perform that action at this time.
0 commit comments