关闭

【Leetcode】Merge k Sorted Lists

241人阅读 评论(0) 收藏 举报
分类:

题目链接:https://leetcode.com/problems/merge-k-sorted-lists/

题目:

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

思路:

注意要保存合并后新表头集合,合并迭代退出的条件是表头集合size为1。空间复杂度为O(n),时间复杂度为O(nlogn)。

设迭代合并链表次数为k,初始链表个数为n,则满足2^k=n,即k=log2(n)。因为每次合并两条链表时间复杂度为O(n),

所以整个算法时间复杂度为O(nlogn)。

算法:

	public ListNode mergeKLists(ListNode[] lists) {
		if (lists.length == 0)
			return null;
		List<ListNode> heads = new ArrayList<ListNode>();// 存放每次合并后的新表头
		for (int i = 0; i < lists.length; i++) { //
			heads.add(lists[i]);
		}
		while (heads.size() > 1) {
			List<ListNode> head2 = new ArrayList<ListNode>();
			for (int i = 0; i + 1 < heads.size(); i += 2) {
				ListNode l1 = heads.get(i);
				ListNode l2 = heads.get(i + 1);
				head2.add(mergeTwoLists(l1, l2));// 合并后新表头集合
			}
			if (heads.size() % 2 == 1) { // 如果表头个数是奇数,要把最后一个表头加进来
				head2.add(heads.get(heads.size() - 1));
			}
			heads = new ArrayList<ListNode>(head2);
		}
		return heads.get(0);
	}

	public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
		ListNode head = null, l, p = l1, q = l2;
		// ==确定头节点
		if (p != null && q != null) {
			if (p.val < q.val) {
				head = p;
				p = p.next;
			} else {
				head = q;
				q = q.next;
			}
		} else if (p == null) {
			head = q;
			return head;
		} else if (q == null) {
			head = p;
			return head;
		}
		l = head;
		// ===end

		while (p != null && q != null) {
			if (p.val < q.val) {
				l.next = p;
				l = p;
				p = p.next;
			} else {
				l.next = q;
				l = q;
				q = q.next;
			}
		}

		if (p == null) {
			l.next = q;
		} else if (q == null) {
			l.next = p;
		}

		return head;
	}


0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

【LeetCode-面试算法经典-Java实现】【023-Merge k Sorted Lists(合并k个排好的的单链表)】

【023-Merge k Sorted Lists(合并k个排好的的单链表)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Merge k sorted linked l...
  • DERRANTCM
  • DERRANTCM
  • 2015-07-23 07:56
  • 1772

LeetCodet题解--23. Merge k Sorted Lists(合并K个已排序的链表)

链接 LeetCode题目:https://leetcode.com/problems/generate-parentheses GitHub代码:https://github.com/...
  • gatieme
  • gatieme
  • 2016-04-08 16:32
  • 1568

LeetCode23 Merge k Sorted Lists 把K个有序链表连接成一个

题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its comple...
  • vvaaiinn
  • vvaaiinn
  • 2015-05-06 11:17
  • 2332

[LeetCode] Merge k Sorted Lists

Merge k Sorted Lists  Merge k sorted linked lists and return it as one sorted list. Analyze and...
  • wangshaner1
  • wangshaner1
  • 2015-04-22 14:56
  • 647

[LeetCode]Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. ...
  • u014691362
  • u014691362
  • 2014-09-02 20:36
  • 353

leetcode23~Merge k Sorted Lists Add to List

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 把...
  • stone_yw
  • stone_yw
  • 2017-02-28 16:19
  • 235

【leetcode】23. Merge k Sorted Lists

合并多个已排序的单链表,多种实现方式
  • lgs770950672
  • lgs770950672
  • 2016-05-26 21:25
  • 237

[leetcode] 23. Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. ...
  • TstsUgeg
  • TstsUgeg
  • 2016-04-07 17:47
  • 320

leetcode_23_Merge k Sorted Lists

思路: 本来想模仿合并两个链表的方法,查找所有的lists链表一遍找出一个最小值,并将最小值链接到结果链表中,并将最小结点删除,将最小结点的next结点替代之,直至为null,将链表删去。 但是当l...
  • dfb198998
  • dfb198998
  • 2015-03-23 20:59
  • 780

LeetCode_OJ【23】Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. ...
  • hb_peng
  • hb_peng
  • 2015-09-16 20:46
  • 260
    个人资料
    • 访问:555585次
    • 积分:8549
    • 等级:
    • 排名:第2649名
    • 原创:305篇
    • 转载:6篇
    • 译文:0篇
    • 评论:37条
    博客专栏
    文章分类
    最新评论