题目:

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.

分析:合并两个有序序列,这个归并排序中的一个关键步骤。这里是要合并两个有序的单链表。由于链表的特殊性,在合并时只需要常量的空间复杂度。

编码:

public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1 == null && l2 == null)
return null;
if(l1 == null && l2 != null)
return l2;
if(l2 == null && l1 != null)
return l1;
ListNode p = l1;
ListNode q = l2;
ListNode newHead = new ListNode(-1);//定义头结点
ListNode r = newHead;
while(p != null && q != null){
if(p.val < q.val){
r.next = p; //这里,将待排序节点直接拼接在新节点后,而不用再创建新节点。节省了空间复杂度。
p = p.next;
r = r.next;
} else {
r.next = q;
q = q.next;
r = r.next;
}
} if(p != null)
r.next = p;
if(q != null)
r.next = q;
return newHead.next;
}

21. Merge Two Sorted Lists (Java 合并有序链表 空间复杂度O(1))的更多相关文章

  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] 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. [LintCode] Merge Two Sorted Lists 混合插入有序链表

    Merge two sorted (ascending) linked lists and return it as a new sorted list. The new sorted list sh ...

  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. 21. Merge Two Sorted Lists[E]合并两个有序链表

    题目 Merge two sorted linked lists and return it as a new list. The new list should be made by splicin ...

  6. LeetCode 21. Merge Two Sorted Lists(合并两个有序链表)

    题意:合并两个有序链表 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next ...

  7. leetcode 21.Merge Two Sorted Lists ,java

    题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...

  8. 21. Merge Two Sorted Lists(合并2个有序链表)

    21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list s ...

  9. 21.Merge Two Sorted Lists 、23. Merge k Sorted Lists

    21.Merge Two Sorted Lists 初始化一个指针作为开头,然后返回这个指针的next class Solution { public: ListNode* mergeTwoLists ...

随机推荐

  1. 【做题】BZOJ2534 L-gap字符串——调和级数

    题意:给出一个字符串,问其中有多少个子串恰好为\(uvu\)的形式.其中,\(u\)非空,\(v\)的长度恰好为\(l\). \(n \leq 5 \times 10^4\) 我们设两个后缀的起点分别 ...

  2. 外观模式Facade pattern

    http://www.runoob.com/design-pattern/facade-pattern.html 外观模式 外观模式(Facade Pattern)隐藏系统的复杂性,并向客户端提供了一 ...

  3. 【Finchley】【升级变更】Spring Cloud 升级到Finchley版本后需要注意的地方

    Spring Boot 2.x 已经发布了很久,现在 Spring Cloud 也发布了 基于 Spring Boot 2.x 的 Finchley 版本,现在一起为项目做一次整体框架升级. 升级前 ...

  4. (zhuan) Speech and Natural Language Processing

    Speech and Natural Language Processing obtain from this link: https://github.com/edobashira/speech-l ...

  5. 【C#】可空类型 NullAble<T>

    在实际编写代码时候 ,  会遇到很多场景, 需要将值置成空, 比如发货日期, 有可能是没有. 在没有可空类型之前, 程序都是用 魔值, 即为一个minValue或者常量, 来代表这个值为空, 也有用一 ...

  6. Quartus工程中各文件类型的含义

    https://blog.csdn.net/jingliangliu/article/details/52245497 .jic           JTAG Indirect Configurati ...

  7. Leetcode_Easy_14

    编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". class Solution { public String longestCommonP ...

  8. mybatis+spring boot, mapper 提示Could not autowire. No beans of … type found

    工具及背景: IntelliJ IDEA 2016.1.3 Ultimate.spring boot, maven项目,利用mybatis 注解的方式查询mysql. 业务逻辑关系:controlle ...

  9. ECMAScript6语法重点(二)

    十一.Proxy和Reflect(Proxy保护对象不被外界访问:Object方法移植到Reflect) ①Proxy:原始对象(供应商)通过Proxy(代理商)生成新对象(映射原对象),用户访问的是 ...

  10. 前端html5/css基础知识

    https://www.cnblogs.com/clschao/articles/10073124.html