Merge K Sorted Lists

5 Approaches

Approach
Time Complexity
Space Complexity

Using Merge 2 Sorted

List

O(nk)O(nk)

O(1)O(1)

Using k pointers

O(nk)O(nk)​

O(1)O(1)

Link All List and Merge Sort

O(nlog⁑n)O(n\log n)

O(1)O(1)

Divide and Conquer

O(nlog⁑k)O(n\log k)​

O(1)O(1)

Discusses 5 Approaches

Using Merge 2 Sorted List

Accepted

Time Complexity: O(kn)O(kn)​ where n n​ is the number of elements in each list.

Space Complexity: O(1)O(1)

Compare All and Choose Min​

Accepted

Time Complexity: O(nk)O(nk)​

Space Complexity: O(1)O(1)

Accepted

Time Complexity: O(nlog⁑k)O(n \log k)​

Space Complexity: O(1)O(1)

Notice Merge Function ⭐​

Dividing k Linked Lists into 2

  1. Divide the given KK Linked Lists into two parts until single LL remains

  2. Backtrack and merge 2 Linked Lists at a time until is reached.

Accepted

Time Complexity: O(nlog⁑k)O(n\log k)​

Space Complexity: O(1)O(1)​

Last updated