-
Notifications
You must be signed in to change notification settings - Fork 0
/
23.merge-k-sorted-lists_2.py
47 lines (42 loc) · 1.14 KB
/
23.merge-k-sorted-lists_2.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
#
# @lc app=leetcode id=23 lang=python
#
# [23] Merge k Sorted Lists
#
from heapq import *
from functools import cmp_to_key
# @lc code=start
# Definition for singly-linked list.
class ListNode(object):
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution(object):
def mergeKLists(self, lists):
"""
:type lists: List[ListNode]
:rtype: ListNode
"""
def mycmp(triplet_left, triplet_right):
key_l, key_r = triplet_left.val, triplet_right.val
if key_l < key_r:
return -1 # larger first
elif key_l == key_r:
return 0 # equal
else:
return 1
WrapperCls = cmp_to_key(mycmp)
pq = []
dummy = ListNode()
curr = dummy
for list in lists:
if list:
heappush(pq, WrapperCls(list))
while pq:
node = heappop(pq).obj
curr.next = node
curr = curr.next
if node.next:
heappush(pq, WrapperCls(node.next))
return dummy.next
# @lc code=end