Skip to content

Commit 0b89fcc

Browse files
author
sejineer
committed
merge-two-sorted-lists solution
1 parent 9984366 commit 0b89fcc

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

merge-two-sorted-lists/sejineer.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""
2+
시간 복잡도: O(n + m)
3+
공간 복잡도: O(1)
4+
"""
5+
class Solution:
6+
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
7+
node = ListNode(0)
8+
head = node
9+
10+
while list1 and list2:
11+
if list1.val <= list2.val:
12+
head.next = list1
13+
list1 = list1.next
14+
else:
15+
head.next = list2
16+
list2 = list2.next
17+
head = head.next
18+
head.next = list1 if list1 else list2
19+
20+
return node.next

0 commit comments

Comments
 (0)