赞
踩
合并两个已排序的数组并不难,在学习 mergesort 的时候应该都有学到,但在 Java 实现合并 LinkedList 中遇到了小小的问题,简短总结下。
首先把合并的算法用伪代码写下来:
public List mergeTwoLists(List l1, List l2) {
List result = new List();
Iterator it1 = l1.iterator();
Iterator it2 = l2.iterator();
while(it1 != null || it2 != null){
if(it1 != null && it2 != null){
if(it1.val < it2.val){
result.add(it1.val);
it1 = it1.next;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。