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 ...
随机推荐
- 51nod-1227-平均最小公倍数
题意 定义 \(n\) 的平均最小公倍数: \[ A(n)=\frac{1}{n}\sum _{i=1}^n\text{lcm}(n,i) \] 求 \[ \sum _{i=L}^RA(i) \] \ ...
- 【loj6038】「雅礼集训 2017 Day5」远行 树的直径+并查集+LCT
题目描述 给你 $n$ 个点,支持 $m$ 次操作,每次为以下两种:连一条边,保证连完后是一棵树/森林:询问一个点能到达的最远的点与该点的距离.强制在线. $n\le 3\times 10^5$ ,$ ...
- Testng 运行Cannot find class in classpath
用Testng运行多个class,结果报: org.testng.TestNGException: Cannot find class in classpath: Salesman at or ...
- BZOJ4921 互质序列
即求删掉一个子序列的gcd之和.注意到前后缀gcd的变化次数都是log级的,于是暴力枚举前缀gcd和后缀gcd即可. #include<iostream> #include<cstd ...
- 笔记-自己看Day20-待续
1. 搭建环境 1)注释掉csrf 2)配置模板路径 'DIRS': [os.path.join(BASE_DIR,'templates')], # BASE_DIR,代指当前目录. 3)配置静态文 ...
- Linux内核设计第三周学习总结 跟踪分析Linux内核的启动过程
陈巧然 原创作品 转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 实验步骤 登陆实验楼虚 ...
- C内存对齐问题-bus error!总线错误!其实是 字符串字面量修改问题!
最近写个小程序,出现bus error! int main(void) { /** * char :1个字节 * char*(即指针变量): 4个字节(32位的寻址空间是2^32, 即32个bit,也 ...
- 网络编程----粘包以及粘包问题的解决、FTP上传
一.粘包现象 让我们基于tcp先制作一个远程执行命令的程序(1:执行错误命令 2:执行ls 3:执行ifconfig) 注意注意: res=subprocess.Popen(cmd.decode('u ...
- opencv2 直方图之calchist函数使用(转)
OpenCV提供了calcHist函数来计算图像直方图. 其中C++的函数原型如下:void calcHist(const Mat* arrays, int narrays, const int* c ...
- Codeforces Round #427 (Div. 2) D dp
D. Palindromic characteristics time limit per test 3 seconds memory limit per test 256 megabytes inp ...