#C14424. Merge Two Lists Alternately
Merge Two Lists Alternately
Merge Two Lists Alternately
Given two lists, merge them by alternatively taking elements from each list. If one list is longer than the other, append the remaining elements at the end.
For example, if the first list is [1, 2, 3]
and the second list is ['a', 'b', 'c', 'd', 'e']
, the merged list would be [1, 'a', 2, 'b', 3, 'c', 'd', 'e']
.
The merging process can be represented mathematically as follows: $$ result[i] = \begin{cases} list1[\frac{i}{2}] & \text{if } i \text{ is even}\\ list2[\frac{i-1}{2}] & \text{if } i \text{ is odd} \end{cases} $$ for \(i = 0,1,2,\ldots, (n+m-1)\), where \(n\) and \(m\) are the lengths of the two lists respectively, and any remaining elements of the longer list are appended at the end.
inputFormat
The input consists of four lines:
- The first line contains an integer \(n\), which is the number of elements in the first list.
- The second line contains \(n\) space-separated tokens (each token is either an integer or a string). If \(n = 0\), this line will be empty.
- The third line contains an integer \(m\), which is the number of elements in the second list.
- The fourth line contains \(m\) space-separated tokens. If \(m = 0\), this line will be empty.
outputFormat
Output a single line containing the merged list elements separated by a single space.
## sample3
1 2 3
5
a b c d e
1 a 2 b 3 c d e