Index: [Leetcode] leetcode key index (C++/JAVA/PYTHON/SQL)
Github:https://github.com/illuz/leetcode
023. Merge k Sorted Lists (Hard)
links:
Title: https://oj.leetcode.com/problems/merge-k-sorted-lists/
Code (GitHub): Https://github.com/illuz/leetcode
Test Instructions:
and 021. Merge two Sorted Lists (easy) is similar, this time to merge K.
Analysis:
It is obvious that you can think of using the function of the completed Merge-Sorted Lists.
There are two ways to do this:
1. (c + +) using the idea of a dichotomy to Merge each list with its adjacent list, so that the scale shrinks by half, and then it can be done with O (NKLOGK). For example: [1, 2, ..., N] The first round of the Merge is [1, N/2], [2, n/2+1], ...
2. (Python) is also the idea of using two points, that is, the Lists is divided into two parts, recursive merge K Sorted Lists after the change into two list, and then the two list of the merge of a Sorted Lists.
Both of these methods are recursive calls, can be memory, with space for time, but I do not know if it will be super-space (memory Limit exceed), so did not try ~
In addition to the idea of two points, there is a better way to write, is to use the heap (heap), specifically, with priority queue.
(Java) First put the first node of each list into the priority queue, remove the maximum node in the queue each time, and then put the node's next in.
Code:
C++:
Class Solution {public: listnode *mergeklists (vector<listnode *> &lists) {int sz = lists.size (); if (sz = = 0) Return Null;while (Sz > 1) {int k = (sz + 1)/2;for (int i = 0; i < SZ/2; i++) lists[i] = mergetwolists (lists[i), Lists[i + K]); sz = k;} return lists[0];} ListNode *mergetwolists (ListNode *l1, ListNode *l2) {if (L1 = = null) return l2;if (L2 = null) return L1; ListNode *start, *p1;if (L1->val < l2->val) {P1 = start = L1;l1 = L1->next;} else {p1 = start = L2;l2 = L2-> ; next;} while (L1 = null && L2! = null) {if (L1->val < l2->val) {P1->next = L1;p1 = L1;l1 = L1->next;} els e {p1->next = L2;P1 = L2;l2 = L2->next;}} if (L1! = NULL) P1->next = L1;elsep1->next = L2;return start; }};
Java:
public class Solution {public ListNode mergeklists (list<listnode> lists) { queue<listnode> heap = New Priorityqueue<listnode> (New comparator<listnode> () { @Override public int compare (ListNode L1, ListNode L2) { return l1.val-l2.val; } }); ListNode dummy = new ListNode (0), cur = dummy, tmp; for (ListNode list:lists) { if (list! = null) { heap.offer (list);} } while (!heap.isempty ()) { tmp = Heap.poll (); Cur.next = tmp; cur = cur.next; if (tmp.next! = null) { heap.offer (tmp.next); } } return dummy.next;} }
Python:
Class solution: # @param a list of ListNode # @return a listnode def mergeklists (self, lists): If Len (list s) = = 0: return None If len (lists) = = 1: return lists[0] mid = len (lists)//2 Left = self.mergeklists (Lists[:mid]) right = Self.mergeklists (lists[mid:]) # Merge left and right dummy = ListNode (0) cur = dummy while le FT or right: if right = = None or (left and Left.val <= right.val): Cur.next = left Left = left.next
else: Cur.next = right Right = right.next cur = cur.next return dummy.next
[Leetcode] 023. Merge k Sorted Lists (Hard) (C++/python)