-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0023.py
48 lines (41 loc) · 1.29 KB
/
0023.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Source: https://leetcode.com/problems/merge-k-sorted-lists
# Title: Merge k Sorted Lists
# Difficulty: Hard
# Author: Mu Yang <http://muyang.pro>
################################################################################################################################
# Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
#
# Example:
#
# Input:
# [
# 1->4->5,
# 1->3->4,
# 2->6
# ]
# Output: 1->1->2->3->4->4->5->6
#
################################################################################################################################
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeKLists(self, lists: List[ListNode]) -> ListNode:
dummy = ListNode(0)
cur = dummy
lists = list(filter(None, lists))
while True:
if not lists:
return dummy.next
i, head = min(enumerate(lists), key=self.keyfunc)
cur.next = head
cur = head
if head.next:
lists[i] = head.next
else:
del lists[i]
@staticmethod
def keyfunc(args):
return args[1].val