Merge Two Sorted Lists
Practice this coding question on Coding Gurukul.
Problem statement
Two service desks each maintain a sorted linked list of appointment times. Their schedules must be combined into one sorted linked list while retaining every appointment, including those with equal timestamps. Your task Merge two nondecreasing singly linked lists by relinking their existing nodes. Return the head of the merged list. Important details Build each input list before merging. Compare current heads and attach the smaller next node; after one list ends, attach the remainder of the other. Do not sort a copied array. Both lists may be empty, and duplicate values must all remain. Worked example Lists 1 -> 5 and 2 -> 5 -> 7 merge into 1 -> 2 -> 5 -> 5 -> 7. The two appointments at time 5 are distinct nodes.
Input format
First line: m n. Then m values for the first list and n values for the second.
Output format
Print the resulting values in order, separated by spaces. Print EMPTY if there are no values.
Constraints
0 <= m, n <= 50; -100 <= values <= 100; each list is sorted.
Sample input
2 3 1 5 2 5 7
Sample output
1 2 5 5 7
Sign in to run or submit a solution in C++, Java or Python.