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相关推荐

  1. 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 ...

  2. (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 ...

  3. 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 ...

  4. 23 Merge k Sorted Lists

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

  5. LeetCode 之 JavaScript 解答第23题 —— 合并K个有序链表(Merge K Sorted Lists)

    Time:2019/4/10 Title: Merge K Sorted Lists Difficulty: Difficulty Author: 小鹿 题目:Merge K Sorted Lists ...

  6. 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 ...

  7. 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 ...

  8. 【LeetCode】【数组归并】Merge k Sorted Lists

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

  9. 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 ...

  10. 合并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 ...

最新文章

  1. 【2021】一位清华大佬的互联网秋招算法岗总结
  2. linux安装ffmpeg
  3. HBase 的存储结构
  4. 【WebRTC---源码篇】(四)WebRTC线程模型
  5. php 中 sql 查询语句怎么写,sql查询语句有哪些
  6. 不到100行写出一个完整的web框架
  7. C#.Net工作笔记014---C#中internal的意思
  8. 可解释性系列论文:Mathematics of Deep Learning
  9. 摄动(Perturbation)
  10. 数据分析的五个阶段及价值
  11. MySQL数据库索引优化实战分析
  12. java+微博上传图片_GitHub - lkshuihua/webpic: 轻量级Java 上传图片 到微博图床小工具...
  13. 动易CMS 复制word里面带图文的文章,图片可以直接显示
  14. navicat如何连接本地数据库
  15. Pika 2.3.5 升级 3.0.16
  16. 图片轮播——Swiper实例
  17. HttpClient 4.2.3 (GA) 这些名字里面都有GA,GA代表什么意思啊?
  18. 陆奇万字长文,讲透企业数字化转型!
  19. 032颜色的调配与三种屏显模式
  20. Fliqlo屏/保mac使用方法

热门文章

  1. 使用 ThinkJS + Vue.js 开发博客系统
  2. Python基础知识梳理(一)
  3. SQL server数据库系统部分常用的存储过程及说明
  4. Ptthon学习记录(八)-------list和元组
  5. 截取指定长度html内容,并保留html格式标记
  6. 配置LANMP环境(2)-- 安装ifconfig命令与安装SecureCRT
  7. Yii2.0 模态弹出框+ajax提交表单
  8. 【Android】全角字符半角字符工具类
  9. HDOJ-2012 素数判定
  10. 比特币分叉第一案,谁动了我的比特币现金?