21.Merge Two Sorted Lists---《剑指offer》面试17
题目链接:https://leetcode.com/problems/merge-two-sorted-lists/description/
题目大意:
给出两个升序链表,将它们归并成一个链表,若有重复结点,都要链接上去,且新链表不新建结点。
法一:直接用数组归并的思想做,碰到一个归并一个,只是要注意链表前后结点之间的操作关系,应该弄清楚java里面引用之间的关系(此题容易面试当场写代码)。代码如下(耗时14ms):
/**
* 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) {
ListNode l = new ListNode(0);
ListNode tmp = l;
while(l1 != null && l2 != null) {
if(l1.val < l2.val) {
l.next = l1;
l1 = l1.next;
// l = l.next;
}
else if(l1.val > l2.val) {
l.next = l2;
l2 = l2.next;
// l = l.next;
}
else {
l.next = l1;
l1 = l1.next;
l = l.next; l.next = l2;
l2 = l2.next;
}
l = l.next;
}
if(l1 != null) {
l.next = l1;
}
else if(l2 != null) {
l.next = l2;
}
l = tmp;
return l.next;
}
}
带有测试代码:
package problem_21; class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
} public class MainTest { public void addNode(ListNode l, int num) {
ListNode listNode = new ListNode(num);
listNode.next = null;
if(l == null) {
l = listNode;
return;
}
ListNode tmp = l;
while(tmp.next != null) {
tmp = tmp.next;
}
tmp.next = listNode;
} public static void main(String[] args) {
MainTest t1 = new MainTest();
ListNode l1 = new ListNode(4);
t1.addNode(l1, 5);
// t1.addNode(l1, 6);
ListNode l2 = new ListNode(4);
t1.addNode(l2, 6);
// t1.addNode(l2, 7);
// while(l1 != null) {
// System.out.println("1val:" + l1.val);
// l1 = l1.next;
// }
// while(l2 != null) {
// System.out.println("2val:" + l2.val);
// l2 = l2.next;
// }
ListNode l = t1.mergeTwoLists(l1, l2);
while(l != null) {
System.out.println(l.val);
l = l.next;
}
} public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode l = new ListNode(0);
ListNode tmp = l;
while(l1 != null && l2 != null) {
if(l1.val < l2.val) {
l.next = l1;//这里在将l1结点赋值给l.next后,不能立即执行l=l1,而只能执行l1=l1.next和l=l.next(这两个位置可以调换)
//至于为什么,还没完全弄明白,应该跟java里面的对象的引用有关系?
// l = l.next;
l1 = l1.next;
}
else if(l1.val > l2.val) {
l.next = l2;
// l = l.next;
l2 = l2.next;
}
else {//测试用例中有5和5归并,结果输出为5,5,所以这里要两次连接结点
l.next = l1;
l = l.next;
l1 = l1.next; l.next = l2;
// l = l.next;
l2 = l2.next;
}
l = l.next;
}
if(l1 != null) {//由于不需要新建结点,所以只需要把剩下的链表结点接上去即可。
l.next = l1;
}
else if(l2 != null) {
l.next = l2;
}
l = tmp;
return l.next;
}
}
法二:递归归并。代码如下(耗时11ms):
public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1 == null) {
return l2;
}
if(l2 == null) {
return l1;
}
ListNode l = null;
if(l1.val < l2.val) {
l = l1;
l.next = mergeTwoLists(l1.next, l2);
}
else {
l = l2;
l.next = mergeTwoLists(l1, l2.next);
}
return l;
}
21.Merge Two Sorted Lists---《剑指offer》面试17的更多相关文章
- 剑指offer 面试17题
面试17题: 题目:打印从1到最大的n位数 题:输入数字n,按顺序打印出从1到最大的n位十进制数,比如输入3,则打印出1.2.3一直到最大的3位数999. 解题思路:需要考虑大数问题,这是题目设置的陷 ...
- [Leetcode][Python]21: Merge Two Sorted Lists
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 21: Merge Two Sorted Listshttps://oj.le ...
- 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【easy】
21. Merge Two Sorted Lists[easy] Merge two sorted linked lists and return it as a new list. The new ...
- 21.Merge Two Sorted Lists 、23. Merge k Sorted Lists
21.Merge Two Sorted Lists 初始化一个指针作为开头,然后返回这个指针的next class Solution { public: ListNode* mergeTwoLists ...
- leetCode练题——21. Merge Two Sorted Lists(照搬大神做法)
1.题目 21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new l ...
- 刷题21. Merge Two Sorted Lists
一.题目说明 这个题目是21. Merge Two Sorted Lists,归并2个已排序的列表.难度是Easy! 二.我的解答 既然是简单的题目,应该一次搞定.确实1次就搞定了,但是性能太差: R ...
- 【一天一道LeetCode】#21. Merge Two Sorted Lists
一天一道LeetCode系列 (一)题目 Merge two sorted linked lists and return it as a new list. The new list should ...
- C# 写 LeetCode easy #21 Merge Two Sorted Lists
21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list s ...
随机推荐
- [codeforces464D]World of Darkraft - 2 概率期望
D. World of Darkraft - 2 time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- 【比赛】HNOI2018 道路
这题很考思维啊,考验我们能否快速从省选难度跳转到普及难度 考试的时候真的想得太多,觉得省选不可能这么简单吧,然后就打脸 设 \(f[i][j][x]\) 表示从根到 \(x\) 号点,有 \(i\) ...
- Codeforces711
A ZS the Coder and Chris the Baboon are travelling to Udayland! To get there, they have to get on th ...
- BZOJ4070 [Apio2015]雅加达的摩天楼 【分块 + 最短路】
题目链接 BZOJ4070 题解 考虑暴力建图,将每个\(B_i\)向其能到的点连边,复杂度\(O(\sum \frac{n}{p_i})\),当\(p\)比较小时不适用 考虑优化建图,每个\(dog ...
- MSSQL代理工作服务器远程命令执行
概述 如果MSSQL数据库中开启了MSSQL Server Agent Job服务的话,攻击者将可以利用MSSQL Server中自带的功能来获取一个shell. SQL Server Agent S ...
- CDN公共库、前端开发常用插件一览表(VendorPluginLib)
=======================================================================================前端CDN公共库===== ...
- 代码收藏系列--mysql--创建数据库、数据表、函数、存储过程命令
创建mysql数据库 CREATE DATABASE IF NOT EXISTS `database_name` DEFAULT CHARSET utf8 COLLATE utf8_general_c ...
- 用Visual C#开发基于OpenCV的Windows应用程序
http://blog.163.com/wangxh_jy/blog/static/28233883201001581640283/ 关于详细的配置及程序运行截图,请下载:http://downloa ...
- GitLab安装部署与管理
一.安装Gitlab前系统预配置准备工作 操作系统:centos 7.3 1.关闭firewalld防火墙 #systemctl stop firewalld //关闭防火墙 #systemctl d ...
- CSK & KCF(tracking)
转自:http://blog.csdn.net/ben_ben_niao/article/details/51364323 上次介绍了SRDCF算法,发展历史轨迹为CSK=>>KCF/DC ...