I’m looking for an elegant, high performance solution to the following problem.
There are 256 linked lists.
- Each list contains the same types of object that among other things holds a whole number that is used to define a sort order.
- All numbers across all lists are unique
- Each individual list is sorted in ascending order by these numbers
How would you create a single ascending ordered list from all the objects from the 256 original linked lists? I’d prefer not to brute force it, and have a few other ideas, but this seems like one of those problems that there’s a standard, optimal solution for.
You could use a priority queue that holds the “topmost” item of each of the 256 linked lists. This “topmost” item is the one that is scheduled to be inserted into the resulting list. This way, you can just take the smallest element from the priority queue, insert it into your resulting queue, and insert its next element into the priority queue: