Quant Memo
Coding/●●●●●

Merge two sorted linked lists

You are given the heads of two singly linked lists, each already sorted ascending. Merge them into one sorted list by splicing the existing nodes together (do not allocate new nodes for the payloads) and return the new head.

l1: 1 -> 2 -> 4
l2: 1 -> 3 -> 4
->  1 -> 1 -> 2 -> 3 -> 4 -> 4

Target O(m+n)O(m + n) time and O(1)O(1) extra space.

Show a hint

Walk both lists with two pointers, always attaching the smaller front node to the result. A dummy head node lets you avoid a special case for choosing the very first element.

Your answer

This one is open-ended. Work it through, then check your reasoning against the full solution.

More Coding questions