Beat 95% c++ by divide-conquer using Merge 2 sorted lists


  • 0
    B
    class Solution {
    public:
        ListNode* mergeKLists(vector<ListNode*>& lists) {
            int n = lists.size();
            if(n==0) return NULL;
            ListNode* head[n];
            for(int i=0;i<n;i++) {
                head[i] = lists[i];
            }
            while(n>1) {
                for(int i=0;i*2<n;i++) {
                    head[i] = mergeTwoLists(head[i*2],(i*2+1) >= n?NULL:head[i*2+1]);
                }
                n = (n+1) / 2;
            }
            return head[0];
        }
    };

Log in to reply
 

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