Merge K Sorted Lists (medium)
Problem Statement #
Given an array of ‘K’ sorted LinkedLists, merge them into one sorted list.
Example 1:
Input: L1=[2, 6, 8], L2=[3, 6, 7], L3=[1, 3, 4]
Output: [1, 2, 3, 3, 4, 6, 6, 7, 8]
Example 2:
Input: L1=[5, 8, 9], L2=[1, 7]
Output: [1, 5, 7, 8, 9]
Try it yourself #
Try solving this question here:
import java.util.*;class ListNode {int value;ListNode next;ListNode(int value) {this.value = value;}}class MergeKSortedLists {public static ListNode merge(ListNode[] lists) {ListNode result = new ListNode(-1);// TODO: Write your code herereturn result;}public static void main(String[] args) {ListNode l1 = new ListNode(2);l1.next = new ListNode(6);l1.next.next = new ListNode(8);ListNode l2 = new ListNode(3);l2.next = new ListNode(6);l2.next.next = new ListNode(7);ListNode l3 = new ListNode(1);l3.next = new ListNode(3);l3.next.next = new ListNode(4);ListNode result = MergeKSortedLists.merge(new ListNode[] { l1, l2, l3 });System.out.print("Here are the elements form the merged list: ");while (result != null) {System.out.print(result.value + " ");result = result.next;}}}
Solution
A brute force solution could be to add all elements of the given ‘K’ lists to one list and sort it. If there are a total of ‘N’ elements in all the input lists, then the brute force solution will have a time complexity of ...