Quick 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.

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

  1. 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.
  2. Settle the empty cases first: if either list is empty, the answer is the other list.
  3. Equal values may come from either list in either order; only the sequence of values is judged, so ties need no special handling.

Merge Two Sorted Linked Lists

mediumCitadelSite Reliability Engineer

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

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

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.

Examples

Example 1
Input
([2, 5, 9], [1, 5, 6, 12])
Output
[1, 2, 5, 5, 6, 9, 12]
Notes
Source Example 1: duplicates across lists (two 5s); head_a runs out first and the trailing 12 of head_b is appended.
Example 2
Input
([-3, 0, 0], [-5, 0, 8])
Output
[-5, -3, 0, 0, 0, 8]
Notes
Source Example 2: negatives and zero, with repeated zeros within and across the lists all kept.

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.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def merge_sorted_linked_lists(head_a: list[int], head_b: list[int]) -> list[int]:
Sample call
merge_sorted_linked_lists([2, 5, 9], [1, 5, 6, 12])
Language
Case 1
Input
([2, 5, 9], [1, 5, 6, 12])
Expected
[1, 2, 5, 5, 6, 9, 12]
Case 2
Input
([-3, 0, 0], [-5, 0, 8])
Expected
[-5, -3, 0, 0, 0, 8]

Browse More Questions

Show the approach

Approach

Two-pointer merge. Keep an index i into head_a and an index j into head_b, both starting at 0. While both lists still have values, compare head_a[i] with head_b[j], append the smaller one (head_a[i] on a tie) to the result, and advance that index. When one list is exhausted, append the rest of the other list unchanged.

Invariant: after each step the result holds the i + j smallest values of the two inputs in non-decreasing order, and its last value is at most every value not yet appended. Because each input is sorted, the smallest unused value overall is at the front of one of the two remaining suffixes, so taking the smaller front keeps the result sorted. Once one list runs out, the other list's remaining suffix is already sorted and every value in it is at least the last value appended, so appending it unchanged completes the merge. Every value is appended exactly once, so the result has len(head_a) + len(head_b) values. A tie may be broken either way because the result is judged only by its sequence of values, which is the same either way.

On real ListNode objects this is the same algorithm: start from a dummy node with a tail pointer, repeatedly attach the smaller front node to tail.next and advance that list, then attach whatever remains of the other list in one step and return dummy.next. That relinks the existing nodes, so it needs only O(1) extra space; a recursive version uses O(n + m) call stack, which is up to 20000 frames here. O(n + m) time cannot be improved, because every node must be placed.

Edge cases: both lists empty returns []; one list empty returns the other unchanged; equal values within and across the lists are all kept (no deduplication); values reach -1000000000 and 1000000000, which fit a 32-bit int, and the algorithm only compares values, so nothing can overflow. Sorting the concatenation also produces the right values in O((n + m) log(n + m)) time but ignores that the inputs are sorted, and the default JavaScript sort compares values as strings, which misorders negative and multi-digit numbers.

Time complexity:
O(n + m)
Space complexity:
O(n + m)