|
| 1 | +class LinkedListNode: |
| 2 | + # __init__ will be used to make a LinkedListNode type object. |
| 3 | + def __init__(self, data, next=None): |
| 4 | + self.data = data |
| 5 | + self.next = next |
| 6 | + |
| 7 | + |
| 8 | +# Template for the linked list |
| 9 | +class LinkedList: |
| 10 | + # __init__ will be used to make a LinkedList type object. |
| 11 | + def __init__(self): |
| 12 | + self.head = None |
| 13 | + |
| 14 | + # insert_node_at_head method will insert a LinkedListNode at head |
| 15 | + # of a linked list. |
| 16 | + def insert_node_at_head(self, node): |
| 17 | + if self.head: |
| 18 | + node.next = self.head |
| 19 | + self.head = node |
| 20 | + else: |
| 21 | + self.head = node |
| 22 | + |
| 23 | + # create_linked_list method will create the linked list using the |
| 24 | + # given integer array with the help of InsertAthead method. |
| 25 | + def create_linked_list(self, lst): |
| 26 | + for x in reversed(lst): |
| 27 | + new_node = LinkedListNode(x) |
| 28 | + self.insert_node_at_head(new_node) |
| 29 | + |
| 30 | + # __str__(self) method will display the elements of linked list. |
| 31 | + def __str__(self): |
| 32 | + result = "" |
| 33 | + temp = self.head |
| 34 | + while temp: |
| 35 | + result += str(temp.data) |
| 36 | + temp = temp.next |
| 37 | + if temp: |
| 38 | + result += ", " |
| 39 | + result += "" |
| 40 | + return result |
| 41 | + |
| 42 | + |
| 43 | +# Template for reversing a linked list |
| 44 | +def reverse_linked_list(head): |
| 45 | + prev, curr = None, head |
| 46 | + while curr: |
| 47 | + nxt = curr.next |
| 48 | + curr.next = prev |
| 49 | + prev = curr |
| 50 | + curr = nxt |
| 51 | + return prev |
| 52 | + |
| 53 | + |
| 54 | +def traverse_linked_list(head): |
| 55 | + current, nxt = head, None |
| 56 | + while current: |
| 57 | + nxt = current.next |
| 58 | + current = nxt |
| 59 | + |
| 60 | + |
| 61 | +# Template for printing the linked list with forward arrows |
| 62 | +def print_list_with_forward_arrow(linked_list_node): |
| 63 | + temp = linked_list_node |
| 64 | + while temp: |
| 65 | + print(temp.data, end=" ") #print node value |
| 66 | + |
| 67 | + temp = temp.next |
| 68 | + if temp: |
| 69 | + print("→", end=" ") |
| 70 | + else: |
| 71 | + print("→ null", end=" ") # if this is the last node, print null at the end |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +#################################################################### |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | +# Helper Function |
| 80 | +def merge_2_lists(head1, head2): |
| 81 | + |
| 82 | + # Assign previous pointer to a dummy node |
| 83 | + dummy = LinkedListNode(-1) |
| 84 | + prev = dummy |
| 85 | + |
| 86 | + # Traverse over the list until both or one becomes null |
| 87 | + while head1 and head2: |
| 88 | + if head1.data <= head2.data: |
| 89 | + |
| 90 | + # if list1 value <= list2 value, add list1 value to list |
| 91 | + prev.next = head1 |
| 92 | + head1 = head1.next |
| 93 | + |
| 94 | + else: |
| 95 | + |
| 96 | + # if list1 value > list2 value, add list2 value to list |
| 97 | + prev.next = head2 |
| 98 | + head2 = head2.next |
| 99 | + |
| 100 | + prev = prev.next |
| 101 | + |
| 102 | + if head1 is not None: |
| 103 | + prev.next = head1 |
| 104 | + else: |
| 105 | + prev.next = head2 |
| 106 | + |
| 107 | + return dummy.next |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | +def merge_k_lists(lists): |
| 112 | + |
| 113 | + if len(lists) > 0: |
| 114 | + step = 1 |
| 115 | + |
| 116 | + while step < len(lists): |
| 117 | + |
| 118 | + for i in range(0, len(lists) - step, step * 2): |
| 119 | + lists[i].head = merge_2_lists(lists[i].head, lists[i + step].head) |
| 120 | + |
| 121 | + step *= 2 |
| 122 | + |
| 123 | + return lists[0].head |
| 124 | + |
| 125 | + return |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | +# Time Complexity = O(nlogk) |
| 130 | +# Space Complexity = O(1) |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | +#################################################################### |
| 135 | + |
| 136 | + |
| 137 | + |
| 138 | +# Driver code |
| 139 | +def main(): |
| 140 | + inputlists = [[[21, 23, 42], [1, 2, 4]], |
| 141 | + [[11, 41, 51], [21, 23, 42]], |
| 142 | + [[2], [1, 2, 4], [25, 56, 66, 72]], |
| 143 | + [[11, 41, 51], [2], [2], [2], [1, 2, 4]], |
| 144 | + [[10, 30], [15, 25], [1, 7], [3, 9], [100, 300], [115, 125], [10, 70], [30, 90]] |
| 145 | + ] |
| 146 | + inp_num = 1 |
| 147 | + for i in inputlists: |
| 148 | + print(inp_num, ".\tInput lists:", sep = "") |
| 149 | + ll_lists = [] |
| 150 | + for x in i: |
| 151 | + a = LinkedList() |
| 152 | + a.create_linked_list(x) |
| 153 | + ll_lists.append(a) |
| 154 | + print("\t", end = "") |
| 155 | + print_list_with_forward_arrow(a.head) |
| 156 | + print() |
| 157 | + inp_num += 1 |
| 158 | + print("\tMerged list: \n\t", end = "") |
| 159 | + print_list_with_forward_arrow(merge_k_lists(ll_lists)) |
| 160 | + print("\n", "-"*100, sep = "") |
| 161 | + |
| 162 | + |
| 163 | +if __name__ == "__main__": |
| 164 | + main() |
0 commit comments