ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

23. Merge k Sorted Lists (分治)

2021-09-07 10:01:31  阅读:137  来源: 互联网

标签:head ListNode lists next Merge l2 Lists l1 Sorted


23. Merge k Sorted Lists

You are given an array of k linked-lists lists, each linked-list is sorted in ascending order.

Merge all the linked-lists into one sorted linked-list and return it.

 

Example 1:

Input: lists = [[1,4,5],[1,3,4],[2,6]]
Output: [1,1,2,3,4,4,5,6]
Explanation: The linked-lists are:
[
  1->4->5,
  1->3->4,
  2->6
]
merging them into one sorted list:
1->1->2->3->4->4->5->6

Example 2:

Input: lists = []
Output: []

Example 3:

Input: lists = [[]]
Output: []

 

Constraints:

  • k == lists.length
  • 0 <= k <= 10^4
  • 0 <= lists[i].length <= 500
  • -10^4 <= lists[i][j] <= 10^4
  • lists[i] is sorted in ascending order.
  • The sum of lists[i].length won't exceed 10^4.

 

 

一、暴力:遍历合并2个有序数组。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode* fakehead = new ListNode(0);
        ListNode* head = fakehead;
        while(l1 != nullptr && l2 != nullptr) {
            if(l1->val < l2->val) {
                head->next = l1;
                l1 = l1->next;            
            } else {
                head->next = l2;
                l2 = l2->next;
            }
            head = head->next;
        }

        if(l1 != nullptr ) {
            head->next = l1;
        }
        if(l2 != nullptr) {
            head->next = l2;
        }
        return fakehead->next;
    }
    ListNode* mergeKLists(vector<ListNode*>& lists) {
        ListNode* head = nullptr;
        for(int i = 0;i < lists.size();++i) {
            head = mergeTwoLists(head,lists[i]);
        }
        return head;
    }
};

 

 

 

二、分治

两两合并

【1,2,3】、【2,4,6】【2,4,6】【1,5,7】

 

【1,2,3】、【2,4,6】  【2,4,6】【1,5,7】

 1,2,2,3,4,6         1,2,4,5,6,7

 

1,1,2,2,2,2,3,4,4,5,6,6,7

 

 

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode* fakehead = new ListNode(0);
        ListNode* head = fakehead;
        while(l1 != nullptr && l2 != nullptr) {
            if(l1->val < l2->val) {
                head->next = l1;
                l1 = l1->next;            
            } else {
                head->next = l2;
                l2 = l2->next;
            }
            head = head->next;
        }

        if(l1 != nullptr ) {
            head->next = l1;
        }
        if(l2 != nullptr) {
            head->next = l2;
        }
        return fakehead->next;
    }
    ListNode* merge(vector<ListNode*>& lists, int low, int high) {
        if(low == high) return lists[low];
        if(low>high) return nullptr;
        int mid = low + (high-low)/2;
        auto a1 = merge(lists,low,mid);
        auto a2 = merge(lists,mid+1,high);
        return mergeTwoLists(a1,a2);
    }

    ListNode* mergeKLists(vector<ListNode*>& lists) {
        return merge(lists,0,lists.size()-1);
    }
};

 

 

标签:head,ListNode,lists,next,Merge,l2,Lists,l1,Sorted
来源: https://www.cnblogs.com/zle1992/p/15236855.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有