LeetCode之Merge Two Sorted Lists
1、题目
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相关推荐
- 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 ...
- 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] 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 ...
- [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 ...
- LeetCode 23. Merge k Sorted Lists
解法一:Priority Queue 优先队列实际是对每次对list头结点依次比较排序的一种优化.插入和删除时间复杂度都为O(logk).一共n个数的话总共是O(nlogk). 复习一下优先队列的写法 ...
- 【LeetCode】Merge Two Sorted Lists(合并两个有序链表)
这道题是LeetCode里的第21道题. 题目描述: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1-&g ...
- LeetCode刷题记录15——21. Merge Two Sorted Lists(easy)
LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) 目录 LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) ...
最新文章
- 【TensorFlow2.0】(7) 张量排序、填充、复制、限幅、坐标选择
- 亚瑟王(bzoj 4008)
- python购物车模块
- python 时间差计算
- padding/border与width的关系
- 2019.08.30数组去重的几种方法以及所需时间对比
- Unity3d学习日记(四)
- LoadRunner 常用C语言函数使用举例说明
- oracle增加表字段_史上最详细的oracle 中的CR块介绍--一致性读
- Flume中的flume-env.sh和log4j.properties配置调整建议(图文详解)
- 全局变量不能放在头文件其中
- 什么是空号检测api接口?哪些行业会用到?
- c语言bim的题目,BIM考试选择题精选
- 思源宋体+Courier New字体
- 计算机网络各层设备故障及可行的解决方案
- 深度强化学习——actor-critic算法(4)
- 对数字签名的攻击概述
- php session 超时设置,php如何使用session 超时设置?
- 格林童话 猫和老鼠做朋友
- 特写|巨头造车:一场生死存亡的跨界战争