We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 8cb65c8 commit 75a496aCopy full SHA for 75a496a
merge-two-sorted-lists/hu6r1s.py
@@ -0,0 +1,26 @@
1
+# Definition for singly-linked list.
2
+# class ListNode:
3
+# def __init__(self, val=0, next=None):
4
+# self.val = val
5
+# self.next = next
6
+class Solution:
7
+ # 시간복잡도: O(n + m) -> 두 리스트의 모든 노드를 한 번씩 방문
8
+ # 공간복잡도: O(1) -> 기존 노드 재배치, 추가 메모리 거의 없음
9
+ def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
10
+ merged_list = ListNode()
11
+ tail = merged_list
12
+
13
+ while list1 and list2:
14
+ if list1.val < list2.val:
15
+ tail.next = list1
16
+ list1 = list1.next
17
+ else:
18
+ tail.next = list2
19
+ list2 = list2.next
20
+ tail = tail.next
21
22
+ if list1:
23
24
25
26
+ return merged_list.next
0 commit comments