Skip to content

Commit fe47f3a

Browse files
author
Jinbeom
committed
Merged Two Sorted Lists Solution
1 parent 00458c7 commit fe47f3a

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

merge-two-sorted-lists/kayden.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
# 시간복잡도: O(N+M) list1:N, list2: M
3+
# 공간복잡도: O(N+M)
4+
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
5+
merged = ListNode()
6+
cur = merged
7+
8+
while list1 and list2:
9+
if list1.val < list2.val:
10+
cur.next = list1
11+
list1 = list1.next
12+
else:
13+
cur.next = list2
14+
list2 = list2.next
15+
temp = temp.next
16+
17+
if list1:
18+
cur.next = list1
19+
20+
if list2:
21+
cur.next = list2
22+
23+
return merged.next

0 commit comments

Comments
 (0)