Press enter to see results or esc to cancel.

Merge K Sorted Lists Leetcode Problem 23 [Python Solution]

If you’ve been working on LeetCode problems for a while, you’ve probably encountered linked list challenges, maybe not exactly Merge K Sorted Lists.

They can range from simple tasks like reversing a linked list to more complex problems like the one we’re going to tackle today: merging k sorted linked lists.

Problem Overview

Let’s start by understanding the problem statement.

You are given an array of k linked-lists.

Each linked-list is sorted in ascending order.

Your task is to merge all these linked-lists into one sorted linked-list and return it.

Here’s an example to illustrate the problem:

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->6

Seems challenging, right?

Don’t worry; we’ll break it down step by step.

Understanding Constraints

Before we dive into solving this problem, let’s understand the constraints.

This information will guide us in choosing an efficient approach.

  • k is the number of linked lists, and its length can vary.
  • Each linked list’s length (lists[i].length) can also vary but is within the range of 0 to 500.
  • The values in the linked lists are within the range of -10^4 to 10^4.
  • Lists are sorted in ascending order.
  • The sum of the lengths of all linked lists will not exceed 10^4. Now that we have a clear understanding of the problem and its constraints, let’s explore different approaches to solving it.

LeetCode Problem Solution

Bruteforce Approach

One way to approach this problem is by taking the simplest path first: merging the lists one by one.

Here’s a Python solution using this brute-force approach:

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def mergeKLists(lists):
    if not lists or len(lists) == 0:
        return None

    while len(lists) > 1:
        mergedLists = []
        for i in range(0, len(lists), 2):
            l1 = lists[i]
            l2 = lists[i + 1] if (i + 1) < len(lists) else None
            mergedLists.append(mergeList(l1, l2))
        lists = mergedLists
    return lists[0]

def mergeList(l1, l2):
    dummy = ListNode()
    tail = dummy

    while l1 and l2:
        if l1.val < l2.val:
            tail.next = l1
            l1 = l1.next
        else:
            tail next = l2
            l2 = l2.next
        tail = tail.next
    if l1:
        tail.next = l1
    if l2:
        tail.next = l2
    return dummy.next

This brute-force approach involves repeatedly merging two linked lists until you have a single merged list.

While it’s a valid solution, it’s not the most efficient one, as it involves iterating through all the nodes repeatedly.

An Efficient Approach with Divide and Conquer

The key to optimizing this problem is to use a divide and conquer strategy.

We can divide the problem into smaller subproblems, solve them, and then combine the results efficiently.

Here’s an efficient Python solution:

def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:

        def mergeList(l1, l2):
            dummy = ListNode()
            tail = dummy

            while l1 and l2:
                if l1.val &lt; l2.val:
                    tail.next = l1
                    l1 = l1.next
                else:
                    tail.next = l2
                    l2 = l2.next
                tail = tail.next
            if l1:
                tail.next = l1
            if l2:
                tail.next = l2
            return dummy.next

        if not lists or len(lists) == 0:
            return None

        while len(lists) > 1:
            mergedLists = []
            for i in range(0, len(lists), 2):
                l1 = lists[i]
                l2 = lists[i + 1] if (i + 1) &lt; len(lists) else None
                mergedLists.append(mergeList(l1, l2))
            lists = mergedLists
        return lists[0]

This efficient approach takes advantage of the divide and conquer strategy, reducing the time complexity to O(n log k), where n is the total number of nodes in all linked lists, and k is the number of linked lists.

It’s a significant improvement over the brute-force approach’s time complexity of O(n * k).

Time and Space Complexity

Now that we’ve discussed the solution, let’s analyze its time and space complexity.

Time Complexity: The time complexity of the efficient approach is O(n log k), where n is the total number of nodes in all linked lists, and k is the number of linked lists.

This complexity arises from the divide and conquer strategy, where we repeatedly merge pairs of linked lists until there’s only one left.

Space Complexity: The space complexity is O(1) since we’re not using any extra data structures that depend on the input size.

Reasoning Behind Our Approach

The reasoning behind the efficient approach lies in the divide and conquer strategy.

By dividing the problem into smaller subproblems and solving them efficiently, we can merge k sorted linked lists in a much more optimized way.

This approach reduces the time complexity from O(n * k) to O(n log k), making it a superior solution for large inputs.

In the divide and conquer approach, we start with the original list of k linked lists.

We repeatedly merge them in pairs, effectively reducing the problem size by half in each iteration.

This strategy allows us to avoid redundant work and ensures that we merge the linked lists efficiently.

Related Interview Questions By Company:

Related Interview Questions By Difficulty:

Conclusion

In this blog post, we’ve tackled the LeetCode problem Merge K Sorted Lists and provided both a brute-force approach and an efficient divide and conquer solution.

We’ve explained the problem, discussed the constraints, and demonstrated how to implement the solutions in Python.

The efficient approach, which utilizes a divide and conquer strategy, significantly improves the time complexity of merging k sorted linked lists.

With a time complexity of O(n log k), it’s a practical and optimized solution for this problem.

If you’re working on similar problems or have any questions or suggestions, please feel free to comment and share your thoughts.

Keep practicing, and happy coding!

Question Link

Now, go ahead and tackle the Merge K Sorted Lists problem on LeetCode with confidence, armed with this efficient solution!

>