-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [Week 10-5] solve merge k sorted lists
- Loading branch information
Showing
1 changed file
with
27 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
""" | ||
Solution: | ||
1) 모든 linked list 의 value 를 arr에 담는다. | ||
2) arr 를 정렬한다. | ||
3) arr 로 linked list 를 만든다. | ||
Time: O(n log(n)) = O(n) arr 만들기 + O(n log(n)) 정렬하기 + O(n) linked list 만들기 | ||
Space: O(n) | ||
""" | ||
|
||
|
||
class Solution: | ||
def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]: | ||
arr = [] | ||
for linked_list in lists: | ||
while linked_list: | ||
arr.append(linked_list.val) | ||
linked_list = linked_list.next | ||
|
||
dummy = ListNode() | ||
node = dummy | ||
|
||
arr.sort() | ||
for num in arr: | ||
node.next = ListNode(num) | ||
node = node.next | ||
return dummy.next |