-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path23_merge_k_sorted_list.py
52 lines (47 loc) · 1.23 KB
/
23_merge_k_sorted_list.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
49
50
51
52
# Definition for singly-linked list.
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
class Solution(object):
def mergeKLists(self, lists):
"""
:type lists: List[ListNode]
:rtype: ListNode
"""
from Queue import PriorityQueue
q = PriorityQueue()
dummy = ListNode(None)
cur = dummy
for l in lists:
if l:
q.put((l.val, l))
while q.qsize() > 0:
cur.next = q.get()[1]
cur = cur.next
if cur.next:
q.put((cur.next.val, cur.next))
return dummy.next
@staticmethod
def createList(arr):
head = None
p = None
for i in arr:
node = ListNode(i)
if head == None:
head = node
p = node
else:
p.next = node
p = p.next
return head
@staticmethod
def printList(p):
while p != None:
print p.val
p = p.next
if __name__ == '__main__':
p1 = Solution.createList([1,3,5,7])
p2 = Solution.createList([2,4,6,8])
p = Solution().mergeKLists([p1, p2])
Solution.printList(p)