merge-two-smerge-two-sorted-listsorted-lists
2019独角兽企业重金招聘Python工程师标准>>>
public class MergeSortListNode {public static void main(String[] args) {// TODO Auto-generated method stubint[] A = {1,3,5,7,9,10,11};int[] B = {2,4,6,8};ListNode listNodeA = null , listNodeB = null;if(A.length > 0){listNodeA = new ListNode(A[0]);}ListNode l1 = listNodeA;if(B.length > 0){listNodeB = new ListNode(B[0]);}ListNode l2 = listNodeB;for(int i = 1 ; i < A.length ; i++){ListNode tmpListNode = new ListNode(A[i]);listNodeA.next = tmpListNode;listNodeA = listNodeA.next;}for(int i = 1 ; i < B.length ; i++){ListNode tmpListNode = new ListNode(B[i]);listNodeB.next = tmpListNode;listNodeB = listNodeB.next;}ListNode res = mergeTwoLists(l1 , l2);
// while(l1 != null){
// System.out.println(l1.val);
// l1 = l1.next;
// }
// while(l2 != null){
// System.out.println(l2.val);
// l2 = l2.next;
// }while(res != null){System.out.println(res.val);res = res.next;}}public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {ListNode s1 , s2;if(l1 == null && l2 != null){return l2;}else if(l1 != null && l2 == null){return l1;}else if(l1 == null && l2 == null){return null;}
// if(l1.val > l2.val){
// s1 = l2;
// s2 = l1;
// }else{
// s1 = l1;
// s2 = l2;
// }s1 = l1;s2 = l2; ListNode tmpRes = s1;ListNode res = null;int startIndex = 0;while(s1 != null && s2 != null){if(s1.val < s2.val){tmpRes = s1;s1 = s1.next;}else{tmpRes = s2;s2 = s2.next;}if(startIndex == 0){res = tmpRes;startIndex++;}System.out.println(tmpRes.val);tmpRes = tmpRes.next;}while(res != null){System.out.println(startIndex + " : " + res.val);res = res.next;}if(s1 != null){tmpRes = s1;}if(s2 != null){tmpRes = s2;}
// while(s2 != null){
// tmpRes.val = s2.val;
// System.out.println(tmpRes.val);
// tmpRes = tmpRes.next;
// s2 = s2.next;
// }return res;}
}
转载于:https://my.oschina.net/LosersAFC/blog/227082
merge-two-smerge-two-sorted-listsorted-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 t ...
- (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 t ...
- 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 t ...
- 23 Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 3 ...
- LeetCode 之 JavaScript 解答第23题 —— 合并K个有序链表(Merge K Sorted Lists)
Time:2019/4/10 Title: Merge K Sorted Lists Difficulty: Difficulty Author: 小鹿 题目: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 t ...
- Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
- 【LeetCode】【数组归并】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(golang实现合并两条已经排序的链表)
21.Merge Two Sorted Lists 题目链接:题目链接 Merge two sorted linked lists and return it as a new list. The n ...
- 合并k个有序链表 python_[LeetCode] 23. Merge k Sorted Lists 合并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. E ...
最新文章
- 【2021】一位清华大佬的互联网秋招算法岗总结
- linux安装ffmpeg
- HBase 的存储结构
- 【WebRTC---源码篇】(四)WebRTC线程模型
- php 中 sql 查询语句怎么写,sql查询语句有哪些
- 不到100行写出一个完整的web框架
- C#.Net工作笔记014---C#中internal的意思
- 可解释性系列论文:Mathematics of Deep Learning
- 摄动(Perturbation)
- 数据分析的五个阶段及价值
- MySQL数据库索引优化实战分析
- java+微博上传图片_GitHub - lkshuihua/webpic: 轻量级Java 上传图片 到微博图床小工具...
- 动易CMS 复制word里面带图文的文章,图片可以直接显示
- navicat如何连接本地数据库
- Pika 2.3.5 升级 3.0.16
- 图片轮播——Swiper实例
- HttpClient 4.2.3 (GA) 这些名字里面都有GA,GA代表什么意思啊?
- 陆奇万字长文,讲透企业数字化转型!
- 032颜色的调配与三种屏显模式
- Fliqlo屏/保mac使用方法