LeetCode 23: Merge K Sorted Lists

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

本题在上一题(LeetCode 21: Merge
Two Sorted Lists)基础上再加深了一步,链表个数从两个改为K个。

此题有如下几种解法:

1、最简单的方法莫过于每次都从K个链表头中找到最小的那个元素,加入结果链表中。基于此,有人通过最小堆来简化最小元素的比较。

 struct CompareListNode{
	bool operator()(const ListNode*p, const ListNode* q)const{
		return p->val > q->val;
	}
};
class Solution {
public:
    ListNode* mergeKLists(vector<ListNode*>& lists) {
       ListNode head(-1);
	vector<ListNode*> vec_helper;
	//除去空链表
	for (int i=0; i<lists.size(); i++)
	{
		if (lists[i]!=NULL)
			vec_helper.push_back(lists[i]);
	}
	//建小堆
	std::make_heap(vec_helper.begin(), vec_helper.end(), CompareListNode());
	ListNode* pTail = &head;
	while (vec_helper.size() > 0)
	{
	    //取最小的元素对应的节点
		ListNode *pNode = vec_helper.front();
		pTail->next= pNode;
		pTail = pTail->next;
		std::pop_heap(vec_helper.begin(), vec_helper.end(),  CompareListNode());
		vec_helper.pop_back();
	    //将该节点之后一个节点加堆中
		if (pNode->next)
		{
			vec_helper.push_back(pNode->next);
			std::push_heap(vec_helper.begin(), vec_helper.end(),  CompareListNode());
		}
	}
	pTail->next = NULL;
	return head.next;
    }
};

同样还有基于优先队列的算法:

	ListNode* mergeKLists3(vector<ListNode*>& lists) {
		int length = lists.size();
		ListNode head(-1);
		ListNode* pTail = &head;
		priority_queue<ListNode*, vector<ListNode*>, CompareListNode> list_queue;
		for (int i=0; i<length; i++)
		{
			if (lists[i])
				list_queue.push(lists[i]);
		}
		while (list_queue.size() >0)
		{
			ListNode* ptmpNode = list_queue.top();
			list_queue.pop();
			pTail->next = ptmpNode;
			pTail = pTail->next;
			if (ptmpNode->next)
				list_queue.push(ptmpNode->next);
		}
		pTail->next = NULL;
		return head.next;
	}

2、每次从数组中取两个链表,将合并结果加入到链表中,反复这个过程,直到数组中只剩一个链表为止,对两个链表进行合并的代码可以复用LeetCode21的代码。

	ListNode* mergeKLists2(vector<ListNode*>& lists) {
		int length = lists.size();
		if (length == 0)
			return NULL;
		while (lists.size() >1)
		{
			ListNode* p1 = lists.front ();
			lists.erase(lists.begin());
			ListNode* p2 = lists.front();
			lists.erase(lists.begin());
                        lists.push_back(mergeTwoLists(p1, p2));
		}
		return lists[0];
	}

3、将数组拆分成左、右两个子数组,递归的对左、右两个子数组进行合并,再将合并得到的两个链表合并在一起。

方法3比较简洁

复用上题两个链表的合并代码,方法3的实现代码如下:

    ListNode* mergeKLists(vector<ListNode*>& lists) {
        int length = lists.size();
        if (length == 0) {
            return NULL;
        } else if(length == 1)
            return lists[0];
        else if(length == 2) {
            return mergeTwoLists(lists[0], lists[1]);
        }
        vector<ListNode*> leftHalf(lists.begin(), lists.begin()+length/2);
        vector<ListNode*> rightHalf(lists.begin()+length/2, lists.end());
        return mergeTwoLists(mergeKLists(leftHalf), mergeKLists(rightHalf));
    }

时间: 2024-10-03 21:53:18

LeetCode 23: Merge K Sorted Lists的相关文章

LeetCode OJ:Merge k Sorted Lists(归并k个链表)

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 类似于归并2个链表,暴力一点的方法就是,每取出一个list就与以前的list归并返回merge后list,知道所有list merge完成. 但是可惜,这样做会TLE.贴下代码先: 1 /** 2 * Definition for singly-linked list. 3 * struct List

LeetCode 21 23:Merge Two Sorted Lists &amp; Merge K Sorted Lists

LeetCode 21: Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 题目分析:对两个有序列表进行合并,这个是数据结构基本题目比较简单,代码如下(这个代码有点长,可以简化): ListNode *mergeTwoL

LeetCode OJ:Merge Two Sorted Lists(合并两个链表)

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 注意题目要求合并的时候不能新建节点,直接使用原来的节点,比较简单,代码如下: 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4

leetcode笔记:Merge Two Sorted Lists

一.题目描述 Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 二.题目分析 这道题题意是,将两个已排序的链表的各个元素进行比较并合并成一个链表,具体思路是,当某一个列表的元素比较小的话,就将其加入到输出链表中,并将该链表的指针指向链表的下一个元素.题目需要注意边界

LeetCode题解之 Merge k Sorted Lists

1.题目描述 2.问题分析 使用合并两个链表的方法,逐次合并,效率较低.可以考虑同时合并K个链表. 3.代码 1 ListNode* mergeKLists(vector<ListNode*>& lists) { 2 3 ListNode *res = NULL; 4 for (vector<ListNode*>::iterator it = lists.begin(); it != lists.end(); it++) { 5 res = mergeTwoLists(re

LeetCode 023 Merge k Sorted Lists

题目要求:Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 分析: 参考网址:http://blog.csdn.net/a83610312/article/details/8554241 思路是:首先将k个链表的第一个节点集合,建堆,然后取出堆顶的节点,链接到结果链表上,然后将该节点的下一个节点入堆,直到所有

[LeetCode]23. Merge k Sorted Lists

23. Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 给定k个排序了的链表,合并k个链表成一个排序链表. 本程序思路: 1)首先得到K个链表的长度和存在len中 2)从K个链表中找到值最小的那个节点,把该节点添加到合并链表中 3)重复len次即可把所有节点添加到合并链表中. 注意事项: 1)K个链表中有的

LeetCode之“链表”:Merge Two Sorted Lists &amp;&amp; Merge k Sorted Lists

1. Merge Two Sorted Lists 题目链接 题目要求:  Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 这道题目题意是要将两个有序的链表合并为一个有序链表.为了编程方便,在程序中引入dummy节点.具体程序如下: 1 /** 2 * Defin

23. Merge k Sorted Lists - LeetCode

Question 23.?Merge k Sorted Lists Solution 题目大意:合并链表数组(每个链表中的元素是有序的),要求合并后的链表也是有序的 思路:遍历链表数组,每次取最小节点 Java实现: public ListNode mergeKLists(ListNode[] lists) { ListNode preHead = new ListNode(0); ListNode minNode = getMinNode(lists); ListNode tmpNode =