#K59127. Merge Two Sorted Linked Lists and Remove Duplicates
Merge Two Sorted Linked Lists and Remove Duplicates
Merge Two Sorted Linked Lists and Remove Duplicates
Given two sorted linked lists (l_1) and (l_2), your task is to merge them into a single sorted linked list containing no duplicate elements. In other words, every element in the resulting list should appear exactly once. The linked lists are provided in sorted order and may contain duplicates. Your solution should merge these lists while eliminating any duplicate values.
Note: You must read the input from standard input (stdin) and output the final merged list to standard output (stdout) as space-separated integers on a single line. If the merged list is empty, nothing should be printed.
inputFormat
The input consists of the following lines:
- An integer \(n\) representing the number of elements in the first linked list.
- A line containing \(n\) space-separated integers in non-decreasing order.
- An integer \(m\) representing the number of elements in the second linked list.
- If \(m > 0\), a line containing \(m\) space-separated integers in non-decreasing order.
If \(n\) or \(m\) is 0, the corresponding list is empty.
outputFormat
Output the merged sorted linked list with duplicates removed as space-separated integers on a single line. If the resulting list is empty, output nothing.
## sample3
1 2 4
4
1 3 4 5
1 2 3 4 5