Skip to content

Commit 97ef058

Browse files
committed
feat: [Week 10-5] solve merge k sorted lists
1 parent 62eaa5c commit 97ef058

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

merge-k-sorted-lists/Chaedie.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
Solution:
3+
1) 모든 linked list 의 value 를 arr에 담는다.
4+
2) arr 를 정렬한다.
5+
3) arr 로 linked list 를 만든다.
6+
7+
Time: O(n log(n)) = O(n) arr 만들기 + O(n log(n)) 정렬하기 + O(n) linked list 만들기
8+
Space: O(n)
9+
"""
10+
11+
12+
class Solution:
13+
def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:
14+
arr = []
15+
for linked_list in lists:
16+
while linked_list:
17+
arr.append(linked_list.val)
18+
linked_list = linked_list.next
19+
20+
dummy = ListNode()
21+
node = dummy
22+
23+
arr.sort()
24+
for num in arr:
25+
node.next = ListNode(num)
26+
node = node.next
27+
return dummy.next

0 commit comments

Comments
 (0)