easyLinked ListPure DSA~14 min
Merge Two Event Logs
Two append-only event logs are each sorted by timestamp and stored as singly linked lists. Merge them into one sorted log, splicing nodes rather than copying values.
Problem
Given the heads of two sorted linked lists, merge them into one sorted list by splicing the nodes together, and return the head of the merged list.
Input
Heads of two lists with a and b nodes (0 ≤ a, b ≤ 10^4), shown as value sequences; values sorted ascending.
Output
The merged value sequence in non-decreasing order.
Constraints
- Both inputs are sorted ascending
- 0 ≤ a, b ≤ 10,000
- Reuse the existing nodes
Examples
Example 1
Input
l1 = [1, 2, 4], l2 = [1, 3, 4]
Output
[1, 1, 2, 3, 4, 4]
Nodes are spliced in non-decreasing order, ties keeping l1 first.
Example 2
Input
l1 = [], l2 = [0]
Output
[0]
One list empty — return the other.