harrytothemoon / leetcodeAplus

Leetcode meeting note
2 stars 0 forks source link

[23] Merge k Sorted Lists #91

Open harrytothemoon opened 3 years ago

harrytothemoon commented 3 years ago
var mergeKLists = function(lists) {
    const arr = [];

    for (let list of lists) {
        while (list) {
            arr.push(list.val);
            list = list.next;
        }
    }

    arr.sort((a,b) => b - a);

    return arr.reduce((acc, val) => acc = new ListNode(val, acc), null);
};