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 ...
随机推荐
- 【VS开发】字符,字节和编码
字符,字节和编码 [原创文章,转载请保留或注明出处:http://www.regexlab.com/zh/encoding.htm] 级别:中级 摘要:本文介绍了字符与编码的发展过程,相关概念的正确理 ...
- 【Linux开发】linux设备驱动归纳总结(七):2.内核定时器
linux设备驱动归纳总结(七):2.内核定时器 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...
- 解决Iframe跨域高度自适应,利用window.postMessage()实现跨域消息传递页面高度(JavaScript)
在iframe跨域引用高度自适应这块写的js方式都试了不管用,最终使用的是window.postMessage() 跨域获取高度 传递信息 1.首先,在主页面上使用iframe引入子页面:也就是A.h ...
- codeforces 620C
题目链接:https://codeforces.com/problemset/problem/620/C 题目分析 题意:给你一串珍珠,每个珍珠都有一个对应值,需要分割这n个珍珠(必须连续),使得每一 ...
- jquery的ajax方法使用application/json出现400错误码的解决方案
400说明是客户端错误,将contentType默认的application/x-www-form-urlencoded改成application/json就出现错误,说明传输的数据不是JSON. 解 ...
- leecode刷题(24)-- 翻转二叉树
leecode刷题(24)-- 翻转二叉树 翻转二叉树 翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 ...
- O008、LVM类型的Storage Pool
参考https://www.cnblogs.com/CloudMan6/p/5277927.html LVM类型的Storage Pool 不仅一个文件可以分配给客户机作为虚拟磁盘,宿主机上 ...
- 上海的Costco,谈谈你的理解和感受
众所周知,Costco在上海第一天开业,由于人流量过大,一度暂停营业.我觉得Costco的成功在于不走寻常路,换位思考(站在用户.厂商角度看问题),下面几点是我觉得它做得比较独特的地方: 1. Cos ...
- whistle 前端工具之抓包利器
一.业务场景 前端本地开发的场景中,我们需要频繁的改动代码,并需要实时看到效果,并且在一些开发场景中,我们需要将特定的请求代理到特定的IP.本地文件等,所以使用fiddler或whistle等本地.真 ...
- Vue+axios 拦截,超时登录问题
axios.interceptors.request.use(config => config, error => Promise.reject(error)); axios.interc ...