You are given an array of k linked-lists lists, each linked-list is sorted in ascending order.Merge all the linked-lists into one sorted linked-list and return it. Example 1:Input: lists = [[1,4,5],[1,3,4],[2,6]]Output: [1,1,2,3,4,4,5,6]Explanation: The linked-lists are:[ 1->4->5, 1->3->4, 2->6]merging them into one sorted list:1->1->2->3->4->4->5->6Example 2:Input: lists = []Output: []Example 3:Input: lists = [[]]Output: [] Constraints:k == lists.length0 <= k <= 1040 <= lists[i].length <= 500-104 <= lists[i][j] <= 104lists[i] is sorted in ascending order.The sum of lists[i].length will not exceed 104.
Question
You are given an array of k linked-lists lists, each linked-list is sorted in ascending order.Merge all the linked-lists into one sorted linked-list and return it. Example 1:Input: lists = [[1,4,5],[1,3,4],[2,6]]Output: [1,1,2,3,4,4,5,6]Explanation: The linked-lists are:[ 1->4->5, 1->3->4, 2->6]merging them into one sorted list:1->1->2->3->4->4->5->6Example 2:Input: lists = []Output: []Example 3:Input: lists = [[]]Output: [] Constraints:k == lists.length0 <= k <= 1040 <= lists[i].length <= 500-104 <= lists[i][j] <= 104lists[i] is sorted in ascending order.The sum of lists[i].length will not exceed 104.
Solution
This problem can be solved using a priority queue in Python. Here are the steps:
-
Initialize a priority queue. In Python, you can use the
heapqlibrary which allows you to use a list as a heap. -
For each linked list in the input, push the first element of the list along with the list itself into the priority queue. The priority queue will automatically sort the elements based on the first element of each list.
-
Pop the smallest element from the priority queue, and add it to the result list. Then, push the next element from the same list into the priority queue. If a list is empty after popping an element, just ignore it.
-
Repeat step 3 until the priority queue is empty.
-
Return the result list.
Here is the Python code for the above steps:
import heapq
def mergeKLists(lists):
heap = [(lst[0], i, 0) for i, lst in enumerate(lists) if lst]
heapq.heapify(heap)
res = []
while heap:
val, list_ind, element_ind = heapq.heappop(heap)
res.append(val)
if element_ind + 1 < len(lists[list_ind]):
next_tuple = (lists[list_ind][element_ind + 1], list_ind, element_ind + 1)
heapq.heappush(heap, next_tuple)
return res
This code works by maintaining a heap of the smallest elements from each list. It repeatedly pops the smallest element from the heap and adds the next element from the same list into the heap, until all elements have been processed. The time complexity is O(N log k), where N is the total number of elements and k is the number of lists, because each insertion into a heap takes log k time and we do this for each of the N elements. The space complexity is O(k), for the heap.
Similar Questions
You are given the heads of two sorted linked lists list1 and list2.Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists.Return the head of the merged linked list. Example 1:Input: list1 = [1,2,4], list2 = [1,3,4]Output: [1,1,2,3,4,4]Example 2:Input: list1 = [], list2 = []Output: []Example 3:Input: list1 = [], list2 = [0]Output: [0] Constraints:The number of nodes in both lists is in the range [0, 50].-100 <= Node.val <= 100Both list1 and list2 are sorted in non-decreasing order.
# Definition for singly-linked list.# class ListNode:# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclass Solution: def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]: result=ListNode()… print(minheap) return result
Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well. Example 1:Input: head = [1,1,2]Output: [1,2]Example 2:Input: head = [1,1,2,3,3]Output: [1,2,3] Constraints:The number of nodes in the list is in the range [0, 300].-100 <= Node.val <= 100The list is guaranteed to be sorted in ascending order.
Given a head to Linked List L, write a function to reverse the list taking k elements at a time. Assume k is a factor of the size of List.You need not have to create a new list. Just reverse the old one using head.
Given a linked list of size N. The task is to reverse every k nodes (where k is an input to the function) in the linked list.Input FormatFirst line of input contains n- the size of first linked list. Next line of input contains n integers the n elements of the first linked list.Constraints1 <= N <= 10^41 <= k <= NOutput FormatPrint the modified linked list.Sample input81 2 2 4 5 6 7 84Sample output4 2 2 1 8 7 6 5
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.