23. Merge k Sorted Lists

本文介绍了一种通过递归二路归并法合并K个已排序链表的方法,并提供了详细的C++代码实现。该算法将K路归并问题转换为两两合并,最终得到一个排序链表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.问题描述

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.

2.解题思路

类似于2路归并法,将K路归并划为2路归并,直到最后得到一个链表,采用递归的方法

3.代码实现

class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
	if (l1 == nullptr)
		return l2;
	else if (l2 == nullptr)
		return l1;
	ListNode *head;	//合并后的头节点
	if (l1->val > l2->val)
	{
		head = l2;
		l2 = l2->next;
	}
	else
	{
		head = l1;
		l1 = l1->next;
	}
	ListNode *r = head;//r保存合并节点的尾节点
					   //逐一合并
	while (l1&&l2)
	{
		if (l1->val <= l2->val)
		{
			r->next = l1;
			l1 = l1->next;
		}
		else
		{
			r->next = l2;
			l2 = l2->next;
		}
		r = r->next;
	}
	if (l1)
		r->next = l1;
	if (l2)
		r->next = l2;
	return head;
}
ListNode* mergeKLists(vector<ListNode*>& lists) {
    if (lists.empty())
		return nullptr;
	if (lists.size() ==1)
		return lists[0];
	int len = lists.size() % 2 + lists.size() / 2;
	vector<ListNode*> v;
	vector<ListNode>::size_type i = 0, j = i + 1;
	while (i < lists.size())
	{
		if (j != lists.size())
			v.push_back(mergeTwoLists(lists[i], lists[j]));
		else
			v.push_back(mergeTwoLists(lists[i], nullptr));
		i += 2;
		j += 2;

	}
	return mergeKLists(v);
}
};

4.复杂度:

总共K个链表,合并K个链表需要logK次,每次时间复杂度为O(m+n),复杂度为O(logK*(m+n))

5.实现截图





To merge k sorted linked lists, one approach is to repeatedly merge two of the linked lists until all k lists have been merged into one. We can use a priority queue to keep track of the minimum element across all k linked lists at any given time. Here's the code to implement this idea: ``` struct ListNode { int val; ListNode* next; ListNode(int x) : val(x), next(NULL) {} }; // Custom comparator for the priority queue struct CompareNode { bool operator()(const ListNode* node1, const ListNode* node2) const { return node1->val > node2->val; } }; ListNode* mergeKLists(vector<ListNode*>& lists) { priority_queue<ListNode*, vector<ListNode*>, CompareNode> pq; for (ListNode* list : lists) { if (list) { pq.push(list); } } ListNode* dummy = new ListNode(-1); ListNode* curr = dummy; while (!pq.empty()) { ListNode* node = pq.top(); pq.pop(); curr->next = node; curr = curr->next; if (node->next) { pq.push(node->next); } } return dummy->next; } ``` We start by initializing a priority queue with all the head nodes of the k linked lists. We use a custom comparator that compares the values of two nodes and returns true if the first node's value is less than the second node's value. We then create a dummy node to serve as the head of the merged linked list, and a current node to keep track of the last node in the merged linked list. We repeatedly pop the minimum node from the priority queue and append it to the merged linked list. If the popped node has a next node, we push it onto the priority queue. Once the priority queue is empty, we return the head of the merged linked list. Note that this implementation has a time complexity of O(n log k), where n is the total number of nodes across all k linked lists, and a space complexity of O(k).
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值