Click here to read more about algorithmic interviewing

Topic describes

You are given an array of linked lists, each of which has been sorted in ascending order.

Please merge all lists into one ascending list and return the merged list. Example 1:

Input: lists = [].enlighened by [1 4], [2, 6]] output:,1,2,3,4,4,5,6 [1] : list array is as follows: [1 - > > 5, 4-1 - > 3 - > 4, 6] 2 - > merge them into an ordered list. 1 - > 1 - > 2 - > 3 - > 4 - > 4 - > 5 - > 6Copy the code

Example 2:

Input: Lists = [] Output: []Copy the code

Example 3:

Input: Lists = [[]] Output: []Copy the code

Tip:

  • k == lists.length
  • 0 <= k <= 10^4
  • 0 <= lists[i].length <= 500
  • -10^4 <= lists[i][j] <= 10^4
  • lists[i]In ascending order
  • lists[i].lengthThe sum of phi is not more than 10 to the fourth

Priority queue solution (Sentry technique)

Let mitsuha remind you of the sentry skills we discussed earlier in the multi-linked list:

A common technique for linked list problems is to add a virtual head node (sentry) to help simplify boundary cases.

Since all lists themselves are “ascending”, an intuitive approach is to compare the headers of each list, select the node with the lowest value, add it to the result, and then update the header of the list as the next pointer to that node. Loop the comparison until all nodes are added to the result.

Corresponding to the code, or do we need to prepare a “set”, put all the head of the linked list node in “collection”, and then each time from the minimum value of “set”, and add the minimum value of the next node into the “collection” (if any), circulating the process until “collection” is empty (that all nodes are processed, Into and out of sets).

A heap is a data structure that satisfies this requirement:

class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        ListNode dummy = new ListNode(-1), tail = dummy;
        PriorityQueue<ListNode> q = new PriorityQueue<>((a, b) -> a.val - b.val);
        for (ListNode node : lists) {
            if(node ! =null) q.add(node);
        }
        while(! q.isEmpty()) { ListNode poll = q.poll(); tail.next = poll; tail = tail.next;if(poll.next ! =null) q.add(poll.next);
        }
        returndummy.next; }}Copy the code
  • Time complexity: Each node is processed once. The complexity is O(n)O(n)O(n)

  • Space complexity: O(1)O(1)O(1)


The last

This is the 23rd article in our “Brush through LeetCode” series, which started on 2021/01/01. As of the start date, there are 1916 questions on LeetCode, some of which have lock questions. We will finish all the questions without lock first.

In this series of articles, in addition to explaining how to solve the problem, I will also present the most concise code possible. If a general solution is involved, the corresponding code template will also be used.

As the number of LeetCode questions keeps increasing with weekly and biweekly competitions, in order to facilitate our progress statistics, we will calculate the progress according to the total number of questions at the beginning of the series as the denominator and the completed questions as the numerator. The current progress is 23/1916.

In order to facilitate the students to debug and submit the code on the computer, I set up the relevant warehouse: Github address & Gitee address.

In the repository, you’ll see links to the series, the corresponding code for the series, the LeetCode source links, and some other preferred solutions.

# Algorithms and data structures #LeetCode # Algorithm interview