Merge k Sorted Linked Lists
You are provided with 'K' sorted linked lists, each sorted in increasing order. Your task is to merge all these lists into one single sorted linked list and return the head of the resultant linked list.
Input:
The first line of input includes an integer 'T' representing the number of queries or test cases.
For each test case:
The first line contains an integer 'K', the number of lists.
The following 'K' lines contain integers, where each line represents the elements of each linked list, separated by a single space and ending with -1.
Output:
For every test case, provide a single line output containing the elements of the merged sorted list, separated by spaces and ending with -1.
The outputs for different test cases should be on separate lines.
Example:
Input:
T = 1
K = 3
1 4 5 -1
1 3 4 -1
2 6 -1
Output:
1 1 2 3 4 4 5 6 -1
Constraints:
1 <= T <= 10
0 <= K <= 103
0 <= length of lists <= 100
-109 <= value of list elements <= 109
andvalue of list elements != -1
Note:
There is no need to print anything; just implement the function to return the required result.
Be the first one to answer
Add answer anonymously...
Top Nagarro SDE interview questions & answers
Popular interview questions of SDE
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app