Python interview question: merge two sorted linked lists
Problem
first and second are linked lists of order times (ListNode: val, next), each sorted ascending; examples show a list as its values. Merge them into one sorted list by relinking the existing nodes, without creating new ones, and return its head.
def merge_order_streams(first: ListNode | None, second: ListNode | None) -> ListNode | None:
Example
first = [5, 12, 40], second = [3, 12, 18, 60] → [3, 5, 12, 12, 18, 40, 60]both 12s stayfirst = [], second = [7] → [7][] stands for None, an empty list
Solve it free
Checked against hidden tests, right in your browser. No sign-up needed.