Eugene Mankovski

1,180
reputation
10
16
let next = null
let head = null
lists = lists.filter(c => c !== null)
while (lists.length) {
    let maxVal = Number.MAX_VALUE
    const nextListIndex = lists.reduce((acc, el, idx) => {
        if (el.val < maxVal) {
            maxVal = el.val
            return idx
        }

        return acc
    }, -1)

    if (nextListIndex >= 0) {
        const el = lists[nextListIndex]

        const node = new ListNode(el.val)
        if (next) {
            next.next = node
            next = node
        } else {
            next = node
        }

        if (!head && next) {
            head = next
        }

        if (!el.next) {
            lists.splice(nextListIndex, 1)
        } else {
            el.val = el.next.val
            el.next = el.next.next
        }
    }
}

return head