My solution based on mergeTwoLists, which is not a recursive method


  • 0

    class Solution {
    public:
    ListNode mergeKLists(vector<ListNode > &lists) {
    int size1 = lists.size();
    if (size1 == 0)
    return NULL;
    while (size1 > 1) {
    int size2 = (size1 + 1) / 2;
    int i = 0;
    for (; i < size2 - 1; ++i)
    lists[i] = mergeTwoLists(lists[2
    i], lists[2
    i+1]);
    if (size1 % 2 == 0)
    lists[i] = mergeTwoLists(lists[2i], lists[2i+1]);
    else
    lists[i] = lists[2*i];
    size1 = size2;
    }
    return lists[0];
    }
    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.