Mark As Completed Discussion

Good afternoon! Here's our prompt for today.

You're given an array of multiple sorted linked lists. Can you write a method that returns it transformed into a single sorted linked list?

Description

Each linked list can be represented as a series of nodes with the following definition:

JAVASCRIPT
1function Node(val) {
2  this.value = val;
3  this.next = null;
4}

Here's how it would be invoked:

JAVASCRIPT
1// Input
2const arr = [
3  2 -> 6 -> 9,
4  1 -> 2 -> 7,
5  2 -> 6 -> 10
6]
7
8mergeLists(arr);
9
10// Output
111 -> 2 -> 2 -> 2 -> 6 -> 6 -> 7 -> 9 -> 10

Constraints

  • Each list can have up to 100000 nodes
  • The cumulative sum of all the nodes in the linked lists will be <= 100000
  • The values in the nodes will be in the range -1000000000 and 1000000000
  • Let n and k be the length of a linked list and number of lists
  • Expected time complexity : O(n*k*log k)
  • Expected space complexity : O(k)

Try to solve this here or in Interactive Mode.

How do I practice this challenge?

PYTHON
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

Here's a video of us explaining the solution.

To change the speed of the video or see it in full screen, click the icons to the right of the progress bar.

Here's our guided, illustrated walk-through.

How do I use this guide?

This is a more difficult problem than it may initially seem!

An obvious brute force solution is simply to:

  1. Traverse through every list
  2. Store all the node values in an array
  3. Sort the array. Unfortunately here, the time complexity is O(n log n) since there are n total nodes and sorting takes O(n log n) time.

We can improve on the brute force approach by instead comparing node to node, and putting them in the right order as we're traversing. This becomes very similar to the Merged Sorted Linked Lists problem.

Step Two

A better approach is to introduce a data structure. We need something that is built to process and order elements one by one in an efficient manner. What about a priority queue?

A priority queue is an abstract data type used to maintain a queue, with each element having a concept of a priority or order to be served.

Let's start by implementing one with a simple insert method:

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

Then, using it is a matter of running the linked lists through it:

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

Don't forget to handle the case when the array is empty:

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

Let's put it all together now.

One Pager Cheat Sheet

  • You can merge multiple sorted linked lists into a single sorted linked list, with time complexity of O(n*k*log k) and space complexity of O(k).
  • We can improve the brute force solution to sorting a list of linked lists by introducing a Priority Queue data structure, which allows us to efficiently process and order elements one by one in O(n log n) time.
  • The process involves running the linked lists through the designated software to achieve the desired result.
  • Always check for an empty array before attempting any operations.
  • Putting it all together is the key to achieving success.

This is our final solution.

To visualize the solution and step through the below code, click Visualize the Solution on the right-side menu or the VISUALIZE button in Interactive Mode.

PYTHON
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

Alright, well done! Try another walk-through.

If you had any problems with this tutorial, check out the main forum thread here.