Splice Two Ascending Linked Lists into a Single Sorted Linked List
Company: Citadel
Role: Site Reliability Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
You are given the heads of two singly linked lists whose values are each sorted in non-decreasing order. Merge them into a single linked list that contains every node of both lists, with values in non-decreasing order, and return its head.
Be ready to state the time and extra-space complexity of your solution: the interviewer asked whether either could be improved.
### Function Signature
```python
def merge_sorted_linked_lists(head_a: Optional[ListNode], head_b: Optional[ListNode]) -> Optional[ListNode]:
```
### Rules
- `ListNode` has an integer field `val` and a field `next` that refers to the following node, or is `None` at the end of the list.
- Either input may be empty, given as `None`. If both are empty, return `None`.
- The result should be made by relinking the nodes of the two input lists.
- When nodes have equal values, either may come first; the result is judged by its sequence of values from head to tail, which is unique.
- In the examples, a linked list is written as its values from head to tail, so `[1, 2, 4]` is the list 1 -> 2 -> 4 and `[]` is `None`.
### Constraints
- `0 <= length of each list <= 10000`
- `-1000000000 <= node.val <= 1000000000`
- Each input list is sorted in non-decreasing order.
### Examples
**Example 1**
```text
Input: head_a = [2, 5, 9], head_b = [1, 5, 6, 12]
Output: [1, 2, 5, 5, 6, 9, 12]
```
Every node from both lists appears once, in non-decreasing order; the two 5s may come from either list in either order.
**Example 2**
```text
Input: head_a = [-3, 0, 0], head_b = [-5, 0, 8]
Output: [-5, -3, 0, 0, 0, 8]
```
Negative values and repeated values within and across the lists are kept.
**Example 3**
```text
Input: head_a = [], head_b = [0, 3]
Output: [0, 3]
```
When one list is empty, the result is the other list.
Overview: Given the heads of two sorted singly linked lists, merge them into one sorted linked list by relinking their nodes and return its head. It tests pointer manipulation, handling of empty lists and duplicate values, and precise reasoning about time and extra-space complexity.
Read the full Citadel Site Reliability Engineer interview experience this question came from
You are given the heads of two singly linked lists, `head_a` and `head_b`, whose values are each sorted in non-decreasing order. Merge them into a single linked list that contains every node of both lists, with values in non-decreasing order, and return its head.
Implement `merge_sorted_linked_lists(head_a, head_b)`.
### Representation
- A linked list is written as its node values from head to tail, so `[1, 2, 4]` is the list `1 -> 2 -> 4` and `[]` is an empty list (`None`). Each argument is given in this form, and you return the merged list in the same form: its values from head to tail.
- In the linked-list setting, the result is made by relinking the nodes of the two input lists. Every node of both inputs appears exactly once, so the result has `len(head_a) + len(head_b)` values.
### Rules
- Either input may be empty. If both are empty, return `[]` (the empty list).
- When nodes have equal values, either may come first; the result is judged by its sequence of values from head to tail, which is unique.
### Constraints
- `0 <= length of each list <= 10000`
- `-1000000000 <= node.val <= 1000000000`
- Each input list is sorted in non-decreasing order.
- Every value fits in a signed 32-bit integer (no value exceeds 2^31 - 1), and the result has at most 20000 values.
### Examples
**Example 1**
```text
Input: head_a = [2, 5, 9], head_b = [1, 5, 6, 12]
Output: [1, 2, 5, 5, 6, 9, 12]
```
Every node from both lists appears once, in non-decreasing order; the two 5s may come from either list in either order.
**Example 2**
```text
Input: head_a = [-3, 0, 0], head_b = [-5, 0, 8]
Output: [-5, -3, 0, 0, 0, 8]
```
Negative values and repeated values within and across the lists are kept.
### Follow-up
Be ready to state the time and extra-space complexity of your solution: the interviewer asked whether either could be improved.
Constraints
- 0 <= length of each list <= 10000
- -1000000000 <= node.val <= 1000000000
- Each input list is sorted in non-decreasing order.
- Every value fits in a signed 32-bit integer; the result has len(head_a) + len(head_b) <= 20000 values.
Examples
Input: ([2, 5, 9], [1, 5, 6, 12])
Expected Output: [1, 2, 5, 5, 6, 9, 12]
Explanation: Source Example 1: duplicates across lists (two 5s); head_a runs out first and the trailing 12 of head_b is appended.
Input: ([-3, 0, 0], [-5, 0, 8])
Expected Output: [-5, -3, 0, 0, 0, 8]
Explanation: Source Example 2: negatives and zero, with repeated zeros within and across the lists all kept.
Hints
- Both inputs are already in non-decreasing order, and every node must appear in the result exactly once, so the result always has len(head_a) + len(head_b) values.
- Settle the empty cases first: if either list is empty, the answer is the other list.
- Equal values may come from either list in either order; only the sequence of values is judged, so ties need no special handling.