Friday, January 29, 2016

LeetCode Q23: Merge k Sorted Lists (Hard)

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.

Solution 1:
The initial idea is very straightforward that every time we take the node with the least number from k lists and link this node to current merged list. To speed up in every round when choosing node with smallest number, I use priority queue.

The code is in following:

Solution 2:
Divide and conque, merge 2 lists at a time. Fastest, 36ms, beat 100%

No comments:

Post a Comment