#C7618. Merge Two Sorted Linked Lists

    ID: 51509 Type: Default 1000ms 256MiB

Merge Two Sorted Linked Lists

Merge Two Sorted Linked Lists

You are given two sorted linked lists represented as sequences of integers. Your task is to merge these two lists into one sorted list. The merged list should maintain the sorted order. In this problem, the input lists are provided as space‐separated numbers on two separate lines. If a line is empty, it represents an empty list.

The merging process should follow the algorithm: \[ \text{Merge}(L_1, L_2) = \begin{cases} L_2 & \text{if } L_1 \text{ is empty} \\ L_1 & \text{if } L_2 \text{ is empty} \\ \min(L_1[0], L_2[0]) \; \text{ followed by merging the rest} \end{cases} \]

Output the merged list as a space separated sequence of integers.

inputFormat

The input consists of two lines:

  • The first line contains a sorted list of integers separated by spaces. It may be empty, indicating an empty linked list.
  • The second line contains another sorted list of integers separated by spaces. It may also be empty.

outputFormat

Output a single line containing the merged sorted list of integers separated by a space. If both input lists are empty, output an empty line.

## sample