LeetCode Merge Two Sorted Lists && 基础算法-合并有序数组
2015-07-08 14:51
696 查看
Quest: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、 l1 is null
2、 l2 is null
3、 both are not null
该题的解法最关键的就是新建一个链头,然后再这个链头的后面添加元素,最后返回链头.next。
测试代码
输出
整个思路和合并数组类似,不同的是合并数组知道长度。
先new一个准备返回的ListNode对象,然后向这个ListNode后面添加。
先遍历其中之一比如l1,将l1的val和l2的val相比较,将小的放在newListNode后面,并更新index
假如l2有剩余,放在后面即可
2、合并有序数组
算法流程:
while(A中还有未遍历元素&&B中也还有未遍历元素){
如果A[i]<B[j]
C[i+j]= A[i];
i++;
否则
C[i+j]=B[j];
j++;
}
while(A中还有未遍历的元素)
C[j+i++]=A[i++];
while(B中还有未遍历的元素)
C[i+j++]=B[j++];
Java 实现:
测试代码
输出
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 13, 15, 17]
合并两个有序数列
题目给出的原型类
public class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } public ListNode mergeTwoLists(ListNode l1, ListNode l2) { }
需要考虑的情况
1、 l1 is null
2、 l2 is null
3、 both are not null
该题的解法最关键的就是新建一个链头,然后再这个链头的后面添加元素,最后返回链头.next。
public ListNode mergeTwoLists(ListNode l1, ListNode l2) { //将l2合并到l1 ListNode newListNode=new ListNode(Integer.MIN_VALUE); ListNode tempListNode=newListNode; ListNode index1=l1; ListNode index2=l2; while(index2!=null) { if(index1!=null)//l1已经没有了 { if(index1.val>index2.val) { tempListNode.next=index2; tempListNode=tempListNode.next; index2=index2.next; } else { tempListNode.next=index1; tempListNode=tempListNode.next; index1=index1.next; } } else//剩下的都是l2 { tempListNode.next=index2; break; } } // if the length of l1 >length of l2 if(index1!=null) { tempListNode.next=index1; } return newListNode.next; }
测试代码
public static void main(String[] args) throws Exception { Main main=new Main(); ListNode l1=main.new ListNode(1); ListNode l2=main.new ListNode(5); ListNode l3=main.new ListNode(10); ListNode l4=main.new ListNode(15); ListNode l5=main.new ListNode(20); l1.next=l2; l2.next=l3; l3.next=l4; l4.next=l5; ListNode l6=main.new ListNode(2); ListNode l7=main.new ListNode(10); ListNode l8=main.new ListNode(11); ListNode l9=main.new ListNode(13); ListNode l10=main.new ListNode(18); l6.next=l7; l7.next=l8; l8.next=l9; l9.next=l10; ListNode l=main.mergeTwoLists(l1, l6); while(true) { if(l!=null) { System.out.println(l.val); l=l.next; } else { break; } } }
输出
1 2 5 10 10 11 13 15 18 20
整个思路和合并数组类似,不同的是合并数组知道长度。
先new一个准备返回的ListNode对象,然后向这个ListNode后面添加。
先遍历其中之一比如l1,将l1的val和l2的val相比较,将小的放在newListNode后面,并更新index
假如l2有剩余,放在后面即可
2、合并有序数组
算法流程:
while(A中还有未遍历元素&&B中也还有未遍历元素){
如果A[i]<B[j]
C[i+j]= A[i];
i++;
否则
C[i+j]=B[j];
j++;
}
while(A中还有未遍历的元素)
C[j+i++]=A[i++];
while(B中还有未遍历的元素)
C[i+j++]=B[j++];
Java 实现:
public static int[] mergeTwoArrays(int [] i1,int [] i2) { int len1=i1.length; int len2=i2.length; int len=len1+len2; int [] n=new int[len]; int index=0,index1=0,index2=0; while(index1<len1 && index2<len2) { if(i1[index1]<i2[index2]) { n[index++]=i1[index1]; index1++; } else { n[index++]=i2[index2]; index2++; } } while(index1<len1) { n[index++]=i1[index1]; index1++; } while(index2<len2) { n[index++]=i2[index2]; index2++; } return n; }
测试代码
int[] i1={1,3,5,7,9,11,13,15,17}; int[] i2={2,4,6,8,10,11}; int[] n=mergeTwoArrays(i1, i2); System.out.println(Arrays.toString(n));
输出
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 13, 15, 17]
相关文章推荐
- 70天复习一次通过信息系统项目管理师考试经验和心得
- abstract factory(抽象工厂) 对象创建型模式
- Prototype pattern (Python recipe)
- uart过来数据不够,组包
- 单例模式
- 数字信号处理公式变程序(四)——巴特沃斯滤波器(下)
- 软件开发过程学习笔记(六)之测试报告模板
- windows 2012 r2下安装sharepoint 2013错误解决
- 软件开发过程学习笔记(六)之测试报告模板 分类: 开发过程 2015-07-08 14:50 8人阅读 评论(0) 收藏
- 判断一个数是否为2的若干次幂
- 实现编辑和删除
- Linux下重启mysql数据库的方法
- ipcs 使用
- C++顺序容器vector、deque、list
- 纯python实现的web: tornado性能测试以及实际使用解析
- linux中的sleep
- .NET 验证码/验证图片
- 计算一个整数中含1的个数
- 通过抽象工厂模式再理解java的上转型
- 大数据平台架构及数据分析平台架构