1、题目

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.
合并2个有序链表

2、代码实现

/*** Definition for singly-linked list.* public class ListNode {*     int val;*     ListNode next;*     ListNode(int x) { val = x; }* }*/
class Solution {public ListNode mergeTwoLists(ListNode l1, ListNode l2) {if (l1 == null) {return l2;}if (l2 == null) {return l1;}ListNode head = new ListNode(0);ListNode cur = head;while (l1 != null && l2 != null) {if (l1.val <= l2.val) {//cur.val = l1.val;这样写会爆空指针异常cur.next = l1;l1 = l1.next;} else {//cur.val = l2.val;这样写会爆空指针异常cur.next = l2;
//              System.out.println(head.val);l2 = l2.next;}cur = cur.next;}if (l1 != null) {cur.next = l1;} else {System.out.println("l2 != null");cur.next = l2;}return head.next;}
}

LeetCode之Merge Two Sorted Lists相关推荐

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

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

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

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

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

  6. [swift] 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. LeetCode 23. Merge k Sorted Lists

    解法一:Priority Queue 优先队列实际是对每次对list头结点依次比较排序的一种优化.插入和删除时间复杂度都为O(logk).一共n个数的话总共是O(nlogk). 复习一下优先队列的写法 ...

  8. 【LeetCode】Merge Two Sorted Lists(合并两个有序链表)

    这道题是LeetCode里的第21道题. 题目描述: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1-&g ...

  9. LeetCode刷题记录15——21. Merge Two Sorted Lists(easy)

    LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) 目录 LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) ...

最新文章

  1. 【TensorFlow2.0】(7) 张量排序、填充、复制、限幅、坐标选择
  2. 亚瑟王(bzoj 4008)
  3. python购物车模块
  4. python 时间差计算
  5. padding/border与width的关系
  6. 2019.08.30数组去重的几种方法以及所需时间对比
  7. Unity3d学习日记(四)
  8. LoadRunner 常用C语言函数使用举例说明
  9. oracle增加表字段_史上最详细的oracle 中的CR块介绍--一致性读
  10. Flume中的flume-env.sh和log4j.properties配置调整建议(图文详解)
  11. 全局变量不能放在头文件其中
  12. 什么是空号检测api接口?哪些行业会用到?
  13. c语言bim的题目,BIM考试选择题精选
  14. 思源宋体+Courier New字体
  15. 计算机网络各层设备故障及可行的解决方案
  16. 深度强化学习——actor-critic算法(4)
  17. 对数字签名的攻击概述
  18. php session 超时设置,php如何使用session 超时设置?
  19. 格林童话 猫和老鼠做朋友
  20. 特写|巨头造车:一场生死存亡的跨界战争

热门文章

  1. 号称迄今为止最快,.NET6带来了什么?
  2. 比较两个字符串的相似度算法
  3. .NET测试用例写的好不好?让变种来测试一下!
  4. SQL Server CDC配合Kafka Connect监听数据变化
  5. 学习搭建 Consul 服务发现与服务网格-有丰富的示例和图片
  6. 说说 C# 9 新特性的实际运用
  7. C# 中 Struct 和 Class 的区别总结
  8. WSL2 支持挂载物理磁盘,Windows 可直接访问 ext4
  9. 回归统计在DMP中的实战应用
  10. dotNET Core 3.X 使用 Web API