题目:

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. [POI2011]Garbage 欧拉回路

    [POI2011]Garbage 链接 https://www.lydsy.com/JudgeOnline/problem.php?id=2278 https://loj.ac/problem/216 ...

  2. bitset,2018蓝桥杯-明码(二进制转换)

    bitset可以存储二进制数位 bitset<8> x(2); cout<<x<<endl; //输出:00000010 #include <iostream ...

  3. CodeForces 430A Points and Segments (easy)(构造)题解

    题意:之前愣是没看懂题意...就是给你n个点的坐标xi,然后还规定了Li,Ri,要求给每个点染色,每一组L,R内的点红色和黑色的个数不能相差大于1个,问你能不能染成功,不能输出-1,能就按照输入的顺序 ...

  4. P3232 [HNOI2013]游走

    吐槽 傻了傻了,对着题解改了好长时间最后发现是自己忘了调用高斯消元了... 思路 期望题,分配编号,显然编号大的分给贡献次数小的,所以需要知道每个边被经过次数的期望 然后边被经过的次数的期望就是连接的 ...

  5. arm中断体系结构

     ARM处理器中有7种类型的异常,按优先级从高到低的排列如下:                   复位异常(Reset).     数据异常(Data Abort).     快速中断异常(FIQ) ...

  6. 第一章(欢迎进入node.js世界)

    本章内容 1:Node.js是什么 2:服务器端javascript 3:node的异步和事件触发本质 4:node为谁而生 5:node程序示例 1.1 node.js他的首次亮相是在2009年,非 ...

  7. Linux 下终端 C 语言控制光标的技巧

    // 清除屏幕 #define CLEAR() printf("\033[2J") // 上移光标 #define MOVEUP(x) printf("\033[%dA& ...

  8. mybatis结合mysql批量操作及查询sql

    MySQL数据库 批量操作主要使用的是Mybatis的foreach,遍历参数列表执行相应的操作,所以批量插入/更新/删除的写法是类似的,只是SQL略有区别而已.MySql批量操作需要数据库连接配置a ...

  9. HDU 5236 Article(概率DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=5236 题意:现在有人要在文本编辑器中输入n个字符,然而这个编辑器有点问题. 在i+0.1s(i>=0)的时 ...

  10. Python学习 day03打卡

    今天学习的主要内容: ppython的基本数据类型: 1. python基本数据类型回顾 2.int---数字类型 4.str---字符串类型 一.python基本数据类型 1. int==>整 ...