3 4 5 6 7 8 9 0 1 2 3 4 5 6 ***** from heapq import heappop, heapreplace, heapify from queue import PriorityQueue # Definition for singly-linked list. class ListNode(object): *** ListNode Class""" def_init__(self, val): self.val = val self.next = None

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
2
3
4
5
6
7
8
9
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
61
62
63
64
65
66
67
68
69
70
71
72
from heapq import heappop, heapreplace, heapify
from queue import PriorityQueue
10
11 class ListNode (object):
12
""" ListNode Class"""
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 def merge_k_lists (lists):
37
""" Merge List """
38
dummy = ListNode (None)
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Definition for singly-linked list.
def __init__(self, val):
self.val = val
self.next = None
def merge_k_lists (lists):
""" Merge Lists """
dummy = node = ListNode (0)
list_h= [(n.val, n) for n in lists if n]
heapify(list_h)
while list_h:
_n_val = list_h[0]
if n_val.next is None:
heappop (list_h) #only change heap size when necessary
else:
heapreplace(list_h, (n_val.next.val, n_val.next))
node.next = n_val
node = node.next
return dummy.next
curr = dummy
q = PriorityQueue ()
for node in lists:
if node:
q.put ((node.val, node))
while not q.empty():
curr.next = q.get() [1] # These two lines seem to
curr = curr.next # be equivalent to :- curr = q.get() [1]
if curr.next:
q.put((curr.next.val, curr.next))
return dummy.next
I think my code's complexity is also 0(nlogk) and not using heap or priority queue,
n means the total elements and k means the size of list.
The merge TwoLists function in my code comes from the problem Merge Two Sorted Lists
whose complexity obviously is 0(n), n is the sum of length of 11 and 12.
To put it simpler, assume the k is 2^x, So the progress of combination is like a full binary tree,
from bottom to top. So on every level of tree, the combination complexity is n,
because every level have all n numbers without repetition.
The level of tree is x, ie log k. So the complexity is 0 (n log k).
for example, 8 ListNode, and the length of every ListNode is x1, x2,
x3, x4, x5, x6, x7, x8, total is n.
on level 3: x1+x2, x3+x4, x5+x6, x7+x8 sum: n
on level 2: x1+x2+x3+x4, x5+x6+x7+x8 sum: n
on level 1: x1+x2+x3+x4+x5+x6+x7+x8 sum: n
Transcribed Image Text:2 3 4 5 6 7 8 9 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 61 62 63 64 65 66 67 68 69 70 71 72 from heapq import heappop, heapreplace, heapify from queue import PriorityQueue 10 11 class ListNode (object): 12 """ ListNode Class""" 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 def merge_k_lists (lists): 37 """ Merge List """ 38 dummy = ListNode (None) 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 # Definition for singly-linked list. def __init__(self, val): self.val = val self.next = None def merge_k_lists (lists): """ Merge Lists """ dummy = node = ListNode (0) list_h= [(n.val, n) for n in lists if n] heapify(list_h) while list_h: _n_val = list_h[0] if n_val.next is None: heappop (list_h) #only change heap size when necessary else: heapreplace(list_h, (n_val.next.val, n_val.next)) node.next = n_val node = node.next return dummy.next curr = dummy q = PriorityQueue () for node in lists: if node: q.put ((node.val, node)) while not q.empty(): curr.next = q.get() [1] # These two lines seem to curr = curr.next # be equivalent to :- curr = q.get() [1] if curr.next: q.put((curr.next.val, curr.next)) return dummy.next I think my code's complexity is also 0(nlogk) and not using heap or priority queue, n means the total elements and k means the size of list. The merge TwoLists function in my code comes from the problem Merge Two Sorted Lists whose complexity obviously is 0(n), n is the sum of length of 11 and 12. To put it simpler, assume the k is 2^x, So the progress of combination is like a full binary tree, from bottom to top. So on every level of tree, the combination complexity is n, because every level have all n numbers without repetition. The level of tree is x, ie log k. So the complexity is 0 (n log k). for example, 8 ListNode, and the length of every ListNode is x1, x2, x3, x4, x5, x6, x7, x8, total is n. on level 3: x1+x2, x3+x4, x5+x6, x7+x8 sum: n on level 2: x1+x2+x3+x4, x5+x6+x7+x8 sum: n on level 1: x1+x2+x3+x4+x5+x6+x7+x8 sum: n
Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY