Question: In this problem, you will use heap to solve a common interview question: Merge sorted lists. Given k linked lists of integer, which are already

In this problem, you will use heap to solve a common interview question: Merge sorted lists.

Given k linked lists of integer, which are already sorted in descending order. Your task is to merge them to one single linked list, and sorted them in descending order. For example, if you are given linked lists: [10, 6, 4, 1], [13, 7, 2], [9, 5, 3, 2, 1], the example output should be [13, 10, 9, 7, 6, 5, 4, 3, 2, 2, 1, 1].

You should use only one heap to solve this problem. HINT: When you push all the head nodes of those linked lists into a max heap, the top node of the heap must be the largest node (the head node of the result linked list).

Useful heap operation: top(), pop(), push(element).

Code in main.cpp:

In this problem, you will use heap to solve a common interviewquestion: Merge sorted lists. Given k linked lists of integer, which are

1 #include val 20 21 22 23 ListNode merge KLists (vector ListNode &lists) priority queue ListNode vector kistNode compare q q is a max-heap of List Mode to compare two ListNode we just compare their "val" members 25 26 for (auto l lists) if (l) q push(l) 28 29 if (q empty return NULL 30 31 32 Your codes here. 33 34 1 #include val 20 21 22 23 ListNode merge KLists (vector ListNode &lists) priority queue ListNode vector kistNode compare q q is a max-heap of List Mode to compare two ListNode we just compare their "val" members 25 26 for (auto l lists) if (l) q push(l) 28 29 if (q empty return NULL 30 31 32 Your codes here. 33 34

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!