题目链接: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的更多相关文章

  1. 剑指offer 面试17题

    面试17题: 题目:打印从1到最大的n位数 题:输入数字n,按顺序打印出从1到最大的n位十进制数,比如输入3,则打印出1.2.3一直到最大的3位数999. 解题思路:需要考虑大数问题,这是题目设置的陷 ...

  2. [Leetcode][Python]21: Merge Two Sorted Lists

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 21: Merge Two Sorted Listshttps://oj.le ...

  3. 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 ...

  4. 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 ...

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

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

  6. 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 ...

  7. 刷题21. Merge Two Sorted Lists

    一.题目说明 这个题目是21. Merge Two Sorted Lists,归并2个已排序的列表.难度是Easy! 二.我的解答 既然是简单的题目,应该一次搞定.确实1次就搞定了,但是性能太差: R ...

  8. 【一天一道LeetCode】#21. Merge Two Sorted Lists

    一天一道LeetCode系列 (一)题目 Merge two sorted linked lists and return it as a new list. The new list should ...

  9. 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 ...

随机推荐

  1. UVA12583_Memory Overow

    题目是很简单的队列维护的题目. 每次加入之前判断该字母是否在队列以及队列的容量是否超过k即可. #include <iostream> #include <cstdio> #i ...

  2. 最大流算法-ISAP

    引入 最大流算法分为两类,一种是增广路算法,一种是预留推进算法.增广路算法包括时间复杂度\(O(nm^2)\)的EK算法,上界为\(O(n^2m)\)的Dinic算法,以及一些其他的算法.EK算法直接 ...

  3. 我的bootstrap学习

    前端开发框架bootstrap Bootstrap 安装     <link ref="stylesheet" href="bs/css/bootstrap.css ...

  4. (转)【Java FTP及FTP服务器搭建】

    转至 http://blog.csdn.net/studyvcmfc/article/details/8147052 目录(?)[+] -[Java FTP及FTP服务器搭建] 一:本文采用apach ...

  5. BZOJ3620 似乎在梦中见过的样子(kmp)

    不是很懂为什么数据范围要开的这么诡异,想到正解都不敢写.用类似NOI2014动物园的方法,对每个后缀求出类似next的数组即可. #include<iostream> #include&l ...

  6. Xor Sum HDU - 4825(01字典序板题)

    #include <iostream> #include <cstdio> #include <sstream> #include <cstring> ...

  7. Win10 安装 Linux 子系统

    Win10 安装 Linux 子系统 因为最近要使用Linux搭服务器,但是用远程的话延迟很烦,用双系统切换很麻烦,用虚拟机又会有点卡,刚好Windows10最近更新了正式版的WSL(windows下 ...

  8. BZOJ3261:最大异或和——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=3261 给定一个非负整数序列{a},初始长度为N. 有M个操作,有以下两种操作类型: 1.A x:添加 ...

  9. 关于使用EmguCV出现 “无法加载 DLL“cvextern”: 找不到指定的程序” 的解决方法

    http://blog.csdn.net/cdjcong/article/details/8444191 查找了网上的一些说法,都是说没有设置好路径,或者未将DLL文件复制到Debug文件夹下,但是我 ...

  10. SpringMVC 重定向

    在返回视图名字的字符串前面加forword:或redirect:前缀是就会对他们做特殊处理,它们分别是转发和重定向 我们测试一个重定向操作把 Java代码 @RequestMapping(" ...