合并k个升序链表-链表23-python&c++

归并思想,两两合并链表

python

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def merge(self, head1, head2):
        pre = curr = ListNode(100)

        while head1 and head2:
            if head1.val <= head2.val:
                curr.next = head1
                curr = curr.next
                head1 = head1.next
            else:
                curr.next = head2
                curr = curr.next
                head2 = head2.next
        
        curr.next = head1 if head1 else head2

        return pre.next
    
    def mergeKLists(self, lists: List[ListNode]):
        n = len(lists)
        if n == 0:
            return None
        if n == 1:
            return lists[0]
        
        mid = n // 2
        a = self.mergeKLists(lists[:mid])
        b = self.mergeKLists(lists[mid:])

        return self.merge(a, b)

堆排序思想

python

# @return ListNode类
#
import heapq

class Solution:
    def mergeKLists(self , lists):
        # write code here
        n = len(lists)
        pre = curr = ListNode(100)
        heap = []
        for index, node in enumerate(lists):
            if node:
                val = node.val
                tup = (val, index, node)
                heapq.heappush(heap, tup)
        
        while heap:
            val, index, node = heapq.heappop(heap)
            curr.next = node
            curr = curr.next
            node = node.next
            if node:
                val = node.val
                tup = (val, index, node)
                heapq.heappush(heap, tup)
        
        return pre.next

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) {
        // 利用最小堆的性质 维护当前的最小节点值
        ListNode* dummy = new ListNode(-1);
        ListNode* pre = dummy;

        function<bool(ListNode*, ListNode*)> cmp = [](ListNode* a, ListNode* b) { return a->val > b->val;};
        priority_queue<ListNode*, vector<ListNode*>, decltype(cmp)> pq(cmp);

        for (auto& head : lists) {
            if (head) pq.push(head);
        }

        while (!pq.empty()) {
            ListNode* curr = pq.top();
            pre->next = curr;
            pre = pre->next;

            pq.pop();
            if (curr->next != nullptr) pq.push(curr->next);
        }

        return dummy->next;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值