File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ Constraints:
3
+ 1. 0 <= list1.length, list2.length <= 50
4
+ 2. -100 <= Node.val <= 100
5
+ 3. list1 and list2 are sorted in non-decreasing order
1
6
7
+ Time Complexity: n๊ณผ m์ด ๊ฐ๊ฐ list1๊ณผ list2์ ๊ธธ์ด๋ฅผ ๋ํ๋ผ ๋, O(n + m)
8
+ - ๊ฐ ๋
ธ๋๋ฅผ ํ ๋ฒ์ฉ๋ง ๋ฐฉ๋ฌธํ๊ธฐ ๋๋ฌธ
9
+
10
+ Space Complexity: O(1)
11
+ - ์๋ก์ด ๋
ธ๋๋ฅผ ๋ง๋ค์ง ์๊ณ ๊ธฐ์กด ๋
ธ๋๋ค์ ์ฐ๊ฒฐ๋ง ๋ฐ๊พธ๊ธฐ ๋๋ฌธ
12
+
13
+ ํ์ด ๋ฐฉ๋ฒ:
14
+ 1. ๋๋ฏธ ๋
ธ๋๋ฅผ ๋ง๋ค์ด์ ๊ฒฐ๊ณผ ๋ฆฌ์คํธ์ ์์์ ์ผ๋ก ์ฌ์ฉ
15
+ 2. ๋ ๋ฆฌ์คํธ๋ฅผ ์์์๋ถํฐ ์ํํ๋ฉด์ ๊ฐ์ ๋น๊ต
16
+ 3. ๋ ์์ ๊ฐ์ ๊ฐ์ง ๋
ธ๋๋ฅผ ๊ฒฐ๊ณผ ๋ฆฌ์คํธ์ ์ฐ๊ฒฐํ๊ณ , ํด๋น ๋ฆฌ์คํธ์ ํฌ์ธํฐ๋ฅผ ๋ค์์ผ๋ก ์ด๋
17
+ 4. ํ์ชฝ ๋ฆฌ์คํธ๊ฐ ๋๋๋ฉด, ๋จ์ ๋ฆฌ์คํธ๋ฅผ ๊ทธ๋๋ก ๊ฒฐ๊ณผ ๋ฆฌ์คํธ ๋ค์ ์ฐ๊ฒฐ
18
+ 5. ๋๋ฏธ ๋
ธ๋์ next๋ฅผ ๋ฐํ (์ค์ ์ ๋ ฌ๋ ๋ฆฌ์คํธ์ ์์์ )
19
+ """
20
+
21
+ # Definition for singly-linked list.
22
+ # class ListNode:
23
+ # def __init__(self, val=0, next=None):
24
+ # self.val = val
25
+ # self.next = next
26
+ class Solution :
27
+ def mergeTwoLists (self , list1 : Optional [ListNode ], list2 : Optional [ListNode ]) -> Optional [ListNode ]:
28
+ result = ListNode ()
29
+ current = result
30
+
31
+ while list1 and list2 :
32
+ if list1 .val <= list2 .val :
33
+ current .next = list1
34
+ list1 = list1 .next
35
+ else :
36
+ current .next = list2
37
+ list2 = list2 .next
38
+ current = current .next
39
+
40
+ if list1 :
41
+ current .next = list1
42
+ if list2 :
43
+ current .next = list2
44
+
45
+ return result .next
You canโt perform that action at this time.
0 commit comments