Leetcode练习题21. Merge Two Sorted Lists
题目描述(easy)
Merge Two Sorted Lists
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.
Example:
Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4
基础解法
在做本题的过程中,由于本人链表这块儿不是很熟悉,所以仿照了Discuss里的解法。思路如下,链表1和链表2是两个
代码如下:
/**
* 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) {
if(l1==null)
{
return l2;
}
if(l2==null)
{
return l1;
}
if(l1.val<=l2.val)
{
ListNode newNode = new ListNode(l1.val);
newNode.next = mergeTwoLists(l1.next,l2);
return newNode;
}
else
{
ListNode newNode = new ListNode(l2.val);
newNode.next = mergeTwoLists(l1,l2.next);
return newNode;
}
}
}
在解决该题目的过程中,大家容易犯的错误有:一是在合并的过程可能出现中断,另一个是存在特殊值的判断问题。
我们分析合并两个链表时,都是从头节点开始。如果链表1的头节点小于链表2的头节点,则链表1的头节点将是合并后链表的头节点。接下来我们继续开始下一轮合并。在两个链表中依然是排序的,因此合并这两个链表的步骤和前面的是一样的。我们还是比较两个头节点的值。如果链表2的头节点小于链表1的头节点的值,因此链表2的头节点的值将是合并剩余节点得到的链表的头节点。

由上图我们可以看到,(a)链表1的头节点的值小于链表2的头节点的值,因此链表1的头节点是合并后链表的头节点。(b)在剩余的节点中,链表2的头节点的值小于链表1的头节点的值,因此链表2的头节点是生育节点的头节点,把这个节点和之前已经合并好的链表的尾节点链接起来。
因此这是一个递归的过程,而递归的停止条件是,当输入第一个的链表为空时,我们只需要返回另外一个链表即可,让它和第二个链表合并。而当输入的第二个链表的为空时,我们只需要返回另外第一个链表即可。
代码优化
我们可以看到,在循环判断的内部,
ListNode newNode = new ListNode(l1.val);
newNode.next = mergeTwoLists(l1.next,l2);
这步的目的是使用一个新的节点来完成链表合并。在该步中,我们让newNode节点等于l1节点的值,实际上我们并不需要再额外创建一个节点等于l1,只需要让原来的l1节点当作头节点即可。省去了每次new新节点的所花的空间。
链表介绍
链表也是线性结构,但是和数组不同,链表中的数据并不存储在连续的内存值中。元素通过指针连接在一起。

链表的优势:
- 动态大小
- 容易删增
劣势:
- 不允许随机访问,只允许顺序访问
- 需要额外的内存来存储指针
表示方法:
链表由指向链表第一个节点的指针表示。链表的节点叫头节点。如果链表为空,则头也为空。
每个节点包含两部分:数据和指针。
public class LinkedList
{
Node head;
class Node
{
int data;
Node next;
Node(int d)
{
data = d;
}
}
}
第一个简单的java语言链表,
代码如下:
public class LinkedList
{
Node head;
static class Node
{
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}
public static void main(String[] args)
{
LinkedList llist = new LinkedList();
llist.head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
llist.head.next = second;
second.next = third;
}
}
其中,Node head是用来声明一个链表的头节点;在class Node 中,声明链表的结构,一个数据data, 一个下一个节点。并声明一个初始化构造器。
在主函数中,我们创建一个链表。并用
- llist.head = new Node(1);创建第一个节点;
- Node second = new Node(2);创建第二个节点并将数据赋值为2;
- Node third = new Node(3);用来创建第三个节点,并将数据赋值为3;

接下来开始连接不同的节点:
llist.head.next = second连接第一个和第二个节点;
second.next = second连接第二个和第三个节点;

链表遍历:
public class LinkedList
{
Node head;
static class Node
{
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}
public static void main(String[] args)
{
LinkedList llist = new LinkedList();
llist.head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
llist.head.next = second;
second.next = third;
llist.printLinkedList();
}
public void printLinkedList()
{
Node n = head;
while(n!=null)
{
System.out.println(n.data);
n = n.next;
}
}
}
其中在链表中,n = head表示指向head的节点。再通过while循环来便利内容打印
Leetcode练习题21. Merge Two Sorted Lists的更多相关文章
- [Leetcode][Python]21: Merge Two Sorted Lists
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 21: Merge Two Sorted Listshttps://oj.le ...
- 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 ...
- 【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...
- 【LeetCode练习题】Merge k Sorted Lists
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- 【一天一道LeetCode】#21. Merge Two Sorted Lists
一天一道LeetCode系列 (一)题目 Merge two sorted linked lists and return it as a new list. The new list should ...
- 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】21. Merge Two Sorted Lists
题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...
- 【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 spli ...
- LeetCode:21. Merge Two Sorted Lists(Easy)
1. 原题链接 https://leetcode.com/problems/merge-two-sorted-lists/description/ 2. 题目要求 给出两个已经从小到大排序的链表ls1 ...
随机推荐
- 【Linux开发】linux设备驱动归纳总结(九):1.platform总线的设备和驱动
linux设备驱动归纳总结(九):1.platform总线的设备和驱动 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...
- mac 上iterm终端连接Linux服务后 中文为乱码问题
https://www.jianshu.com/p/8b00f71b2177 编辑 ssh 配置vim /etc/ssh/ssh_config修改如下:Host *#SendEnv LANG LC_* ...
- PTA(Basic Level)1053.住房空置率
在不打扰居民的前提下,统计住房空置率的一种方法是根据每户用电量的连续变化规律进行判断.判断方法如下: 在观察期内,若存在超过一半的日子用电量低于某给定的阈值 e,则该住房为"可能空置&quo ...
- yum源配置、epel源配置
关键词:yum源,本地yum源,网络yum源 [1]配置本地yum源 1.挂载好光盘到/redhat/mnt/mnt下 mount /dev/cdrom /mnt 2.操作 cd /etc/yum ...
- SSH框架aop的切面表达式
一:概述 众所周知,Spring是一个轻量级的.非侵入式的.独立于各种应用服务器的开源框架.它的两大方面被人们所熟知,也应用很广.那就是IOC(控制反转)和AOP(面向方面编程). IOC是开发者不创 ...
- Redis缓存击穿
缓存击穿 缓存击穿,是指一个key非常热点,在不停的扛着大并发,大并发集中对这一个点进行访问,当这个key在失效的瞬间,持续的大并发就穿破缓存,直接请求数据库,就像在一个屏障上凿开了一个洞. 比如在做 ...
- 双指针(最大删除子串)Codeforces Round #579 (Div. 3)--Remove the Substring (hard version)
题目链接:https://codeforces.com/contest/1203/problem/D2 题意: 给你S串.T串,问你最长删除多长的子串使得S串里仍然有T的子序列. 思路: 想了好久,先 ...
- 剑指offer-数组中只出现一次的数字-数组-python
题目描述 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. # -*- coding:utf-8 -*- class Solution: # 返回[a, ...
- SpringMVC整体架构
总结: 1. 用户发起请求到前端控制器(DispatchServlet): 2. 前端控制器没有处理业务逻辑的能力,需要找到具体的模型对象处理(Handler),到处理器映射器中查找Handler对象 ...
- wepy 使用filter过滤器
作为过滤器,filter的好处不言而喻,使用过vue的雄蝶内心乐开了花 那么在wepy中,遇到需要计算的数据,要如何使用filter去处理呢 新建.wxs文件 文件名称.位置自己看着来,当然能够一目了 ...