【力扣】(困难)23 合并K个升序链表 --- 分治思想

  • 23 合并K个升序链表
  • 来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/merge-k-sorted-lists
  • 给你一个链表数组,每个链表都已经按升序排列。
    请你将所有链表合并到一个升序链表中,返回合并后的链表。
示例 1:

输入:lists = [[1,4,5],[1,3,4],[2,6]]
输出:[1,1,2,3,4,4,5,6]
解释:链表数组如下:
[
  1->4->5,
  1->3->4,
  2->6
]
将它们合并到一个有序链表中得到。
1->1->2->3->4->4->5->6

解题思路:【python 3】(1)逐个合并:这个题在合并两个有序链表的基础上做,result为最终链表,遍历lists所有链表,逐个与result合并。 (2)分治合并:两两链表合并得到个数减少一半的若干新链表,再执行两两合并,一直合并到只剩下一个链表即最终链表。分治采用二分法划分,入口函数为merge(),其中参数left和right分别是链表数组[left,right]的首尾下标,mid=left+(right-left)/2,当left==right时,返回链表lists[left],当left>right时,返回空,当left

# Python 3
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def mergeKLists(self, lists: List[ListNode]) -> ListNode:
        def mergeTwoLists(l1, l2):
            head = ListNode()
            cur = head
            while l1 and l2:
                if l1.val<l2.val:
                    cur.next = l1
                    l1 = l1.next
                else:
                    cur.next=l2
                    l2 = l2.next
                cur =cur.next
            if l1:  cur.next = l1
            if l2:  cur.next =l2
            return head.next
        # solution 1: 逐个合并
        # if len(lists)==0 : return None
        # i = 1
        # result = lists[0]
        # while i
        #     result = mergeTwoLists(result, lists[i])
        #     i +=1
        # return result

        # solution 2:分治合并
        def merge(seg_lists, left, right):
            if left == right: return seg_lists[left]
            if left > right: return None
            mid = left + (right-left)//2
            return mergeTwoLists(merge(seg_lists, left, mid), merge(seg_lists, mid+1, right))

        if len(lists)==0 : return None
        return merge(lists, 0, len(lists)-1)
// c++
/**
 * 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* mergeKLists(vector<ListNode*>& lists) {
     
        if (lists.size()==0)  return nullptr;
        return merge(lists, 0, lists.size()-1);
    }
    ListNode* merge(vector<ListNode*> lists, int left, int right){
     
        if (left==right) return lists[left];
        if (left>right) return nullptr;
        int mid = left +(right-left)/2;
        return mergeTwoLists(merge(lists, left, mid), merge(lists, mid+1, right));
    }

    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2){
     
        // if (l1==nullptr && l2==nullptr) return nullptr;
        // if (l1==nullptr && l2!=nullptr) return l2;
        // if (l1!=nullptr && l2==nullptr) return l1;
        // ListNode *cur = new ListNode();
        // // ListNode *cur =head;
        // if(l1->val < l2->val){
     
        //     cur->val = l1->val;
        //     cur->next = mergeTwoLists(l1->next, l2);
        // }
        // else{
     
        //     cur->val = l2->val;
        //     cur->next = mergeTwoLists(l1, l2->next);
        // }
        // return cur;

        ListNode *head = new ListNode();
        ListNode *cur =head;
        while(l1!=nullptr && l2!=nullptr){
     
            if(l1->val < l2->val){
     
                cur->next = l1;
                l1 = l1->next;
            }else{
     
                cur->next = l2;
                l2 = l2->next;
            }
            cur = cur->next;
        }
        if(l1!=nullptr) cur->next =l1;
        if(l2!=nullptr) cur->next =l2;
        return head->next;
    }
};

你可能感兴趣的:(链表,分治思想,leetcode,链表)