LeetCode Merge K Sorted Lists

本文详细介绍了如何使用归并合并的思想,通过递归和List<ListNode>的subList方法,高效地合并k个已排序的单链表。重点突出了算法的核心思想和优化策略,旨在提升合并效率,避免了超时问题。

题目:

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

题意:

考虑将k个已经排好序的单链表合并成一个单链表。此题和Merge Two Sorted Lists(https://leetcode.com/problems/merge-two-sorted-lists/)有异曲同工之妙,就是说,都是要合并已经排好序的单链表。那么此题我们考虑在合并两个已经排好序的单链表的那题的基础上来重新修改代码。首先我们能够想到的一种方法是采用遍历一遍单链表数组,依次两两将相邻的两个单链表合并,但是这样的后果就是Time Limit Exceeded,超时。超时的代码如下:

public ListNode mergeKLists(ListNode[] lists)
	{
		if(lists.length == 0 || lists == null)
			return null;
		else if(lists.length == 1)
			return lists[0];
		else 
		{
			ListNode l = lists[0];
			for(int i = 1; i < lists.length; i++)
				l = mergeTwoLists(l,lists[i]);
			return l;
		}
	}
	public static ListNode mergeTwoLists(ListNode l1,ListNode l2)   //两个已经排好序的单链表合并
	{
		ListNode node,list;
		ListNode head = null;
		if(l1 == null && l2 == null)
		{
			return null;
		}
		else if(l1 == null && l2 != null)
			return l2;
		else if(l1 != null && l2 == null)
			return l1;
		else
		{
			if(l1.val <= l2.val)
			{
				node = l1;
				head = node;
				l1 = l1.next;
			}
			else
			{
				node = l2;
				head = node;
				l2 = l2.next;
			}
			while(l1 != null && l2 != null)
			{
				if(l1.val <= l2.val)
				{
					node.next = l1;
					node = node.next;
					l1 = l1.next;
				}
				else
				{
					node.next = l2;
					node = node.next;
					l2 = l2.next;
				}
			}
			if(l1 == null && l2 != null)
			{
				while(l2 != null)
				{
					node.next = l2;
					node = node.next;
					l2 = l2.next;
				}
			}
			else if(l1 != null && l2 == null)
			{
				while(l1 != null)
				{
					node.next = l1;
					node = node.next;
					l1 = l1.next;
				}
			}
			return head;
		}
	}
那么就不能这么简单地做,于是考虑用归并合并来做,用递归将整个单链表数组分成两部分,然后不停地分,不停地合并,注意在这个代码中,运用了List<ListNode>这个链表,主要是想要运用它的subList方法,可以很方便地截取List中的某一部分。最后将取到的最后两个List再来一次两两合并即可。

class ListNode
{
	int val;
	ListNode next;
	ListNode(int x)
	{
		val = x;
	}
}

public class mergeLists 
{
	public ListNode mergeKLists(ListNode[] lists)
	{
		if(lists == null || lists.length == 0)
			return null;
		List<ListNode> list = new ArrayList<ListNode>();
		for(int i = 0; i < lists.length; i++)
			list.add(lists[i]);
		
		return mergeklists(list);
	}
	public ListNode mergeklists(List<ListNode> list)
	{
		int length = list.size();
		if(list == null || length == 0)
			return null;
		else if(length == 1)
			return list.get(0);
		else
		{
			int mid = (length - 1) / 2;
			ListNode l1 = mergeklists(list.subList(0,mid + 1));
			ListNode l2 = mergeklists(list.subList(mid + 1,length));
			return mergeTwoLists(l1,l2);
		}
	}
	public static ListNode mergeTwoLists(ListNode l1,ListNode l2)   //两个已经排好序的单链表合并
	{
		ListNode node,list;
		ListNode head = null;
		if(l1 == null && l2 == null)
		{
			return null;
		}
		else if(l1 == null && l2 != null)
			return l2;
		else if(l1 != null && l2 == null)
			return l1;
		else
		{
			if(l1.val <= l2.val)
			{
				node = l1;
				head = node;
				l1 = l1.next;
			}
			else
			{
				node = l2;
				head = node;
				l2 = l2.next;
			}
			while(l1 != null && l2 != null)
			{
				if(l1.val <= l2.val)
				{
					node.next = l1;
					node = node.next;
					l1 = l1.next;
				}
				else
				{
					node.next = l2;
					node = node.next;
					l2 = l2.next;
				}
			}
			if(l1 == null && l2 != null)
			{
				while(l2 != null)
				{
					node.next = l2;
					node = node.next;
					l2 = l2.next;
				}
			}
			else if(l1 != null && l2 == null)
			{
				while(l1 != null)
				{
					node.next = l1;
					node = node.next;
					l1 = l1.next;
				}
			}
			return head;
		}
	}
}
主要是学习归并合并的思想。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值