21. Merge Two Sorted Lists (Java 合并有序链表 空间复杂度O(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.
分析:合并两个有序序列,这个归并排序中的一个关键步骤。这里是要合并两个有序的单链表。由于链表的特殊性,在合并时只需要常量的空间复杂度。
编码:
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))的更多相关文章
- [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] 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 ...
- [LintCode] Merge Two Sorted Lists 混合插入有序链表
Merge two sorted (ascending) linked lists and return it as a new sorted list. The new sorted list sh ...
- 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 ...
- 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 ...
- LeetCode 21. Merge Two Sorted Lists(合并两个有序链表)
题意:合并两个有序链表 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next ...
- 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 ...
- 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 ...
- 21.Merge Two Sorted Lists 、23. Merge k Sorted Lists
21.Merge Two Sorted Lists 初始化一个指针作为开头,然后返回这个指针的next class Solution { public: ListNode* mergeTwoLists ...
随机推荐
- MySQL 5.7的原生JSON数据类型使用
新增测试用表: CREATE TABLE lnmp ( `id` ) unsigned NOT NULL AUTO_INCREMENT, `category` JSON, `tags` JSON, P ...
- hexo在github和coding.net部署并分流(一)
安装GIT和Node.JS 首先在自己的电脑上安装好git和node.js,这一步怎么做自己搜索,安装软件都是下一步下一步,应该不难,GIT安装完成后打开git cmd输入 git config -- ...
- 这几道Java集合框架面试题几乎必问
Arraylist 与 LinkedList 异同 补充:数据结构基础之双向链表 ArrayList 与 Vector 区别 HashMap的底层实现 JDK1.8之前 JDK1.8之后 HashMa ...
- 解决Maven管理项目update Maven时,jre自动变为1.5
本文为博主原创,未经允许不得转载: 在搭建一个maven web项目时,项目已经按步骤搭建完好,之后项目上就报了一个错误. 在控制台看到错误提示如下:Dynamic Web Module 3.0 re ...
- WijmoJS 全面支持 Angular 7
概述 首先恭喜Angular团队发布Angular 7.0.0版本! 对于大多数开发人员,只需要执行一个命令就可以更新到Angular 7: ng update \@angular/cli \@ang ...
- Sublime Text3 插件:DocBlockr与javascript注释规范
原:http://www.ithao123.cn/content-719950.html 1.引子 在写代码的时候,尤其是写脚本,最需要注释了.目前脚本.样式的注释格式都有一个已经成文的约定规范(这些 ...
- HDU 5988 Coding Contest(浮点数费用流)
http://acm.split.hdu.edu.cn/showproblem.php?pid=5988 题意:在acm比赛的时候有多个桌子,桌子与桌子之间都有线路相连,每个桌子上会有一些人和一些食物 ...
- JqGrid 列时间格式化
{name:'createTime',index:'createTime',label:"创建时间", editable:false,formatter:"date&qu ...
- 清除memcached缓存
telnet localhost 11211 flush_all 最后要一定要关闭dos窗体,不然会导致memcached写值返回ture,但是实际上并没有写入值
- 【selenium2】【selenium基础语法】
#栗子 设置浏览器窗口大小 driver.set_window_size(480,800) #栗子 设置浏览器大小为最大maximize_window() 控制浏览器前进.后退 #栗子from sel ...