Skip to content

Commit

Permalink
feat: [Week 10-5] solve merge k sorted lists
Browse files Browse the repository at this point in the history
  • Loading branch information
Chaedie committed Feb 14, 2025
1 parent 62eaa5c commit 97ef058
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions merge-k-sorted-lists/Chaedie.py
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

0 comments on commit 97ef058

Please sign in to comment.