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 together the nodes of the first two lists.
合并两个有序的链表并将其作为新链表返回。 新链表应通过将前两个列表的节点拼接在一起。
public ListNode mergeTwoLists(ListNode l1,ListNode l2){
ListNode newNode; //new一个新的节点作新的链表
if (l1 == null && l2 == null) {
return null;
}
if (l1 == null) { //如果L1为空,即L2长度大于L1,所以返回L2剩余节点
newNode = l2;
return newNode;
}
if (l2 == null) { //与上同理
newNode = l1;
return newNode;
}
if (l1.val > l2.val) {
newNode = l2;
l2 = l2.next;
} else {
newNode = l1;
l1 = l1.next;
}
newNode.next = mergeTwoLists(l1, l2); //采取递归思想
return newNode;
}
LeetCode记录之21——Merge Two Sorted Lists的更多相关文章
- 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 ...
- [LeetCode&Python] Problem 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❤python】21. Merge Two Sorted Lists
#-*- coding: UTF-8 -*- # Definition for singly-linked list.# class ListNode(object):# def __init ...
- [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 ...
- 刷题21. Merge Two Sorted Lists
一.题目说明 这个题目是21. Merge Two Sorted Lists,归并2个已排序的列表.难度是Easy! 二.我的解答 既然是简单的题目,应该一次搞定.确实1次就搞定了,但是性能太差: R ...
- [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 ...
随机推荐
- c++多线程编程(二)
这是道面试题目:有三个线程分别打印A.B.C,请用多线程编程实现,在屏幕上循环打印10次ABCABC… 见代码: #include <iostream> #include <Wind ...
- c++ (proxy)代理模式
假设我们有几个具有相似的窗体,都包含关闭窗体(closeButton)和按钮单击事件(ClickButton)我们在处理时,不想直接操作每个窗体,可以请求代理. #include<iostrea ...
- 1-element.src.match("bulbon")
element.src.match("bulbon")这里的math里面”bulbon“是什么意思? 原代码中 if (element.src.match("bulbon ...
- C++ 输出精度和输出小数点位数
有时候需要调节小数点的精度或者位数 #include<iostream> #include<iomanip> using namespace std; //设置数据精度 set ...
- jq一行一行循环读取table中的元素
获取当前tr行号,可依据index 获取当前tr对象 获取某一tr下td的内容
- HTML中meta标签的作用与使用
META标签用来描述一个HTML网页文档的属性 META标签可分为两大部分:HTTP-EQUIV和NAME变量. HTTP实例 HTML代码实例中有一项内容是 <meta http-equiv= ...
- Django框架 之 Form表单和Ajax上传文件
Django框架 之 Form表单和Ajax上传文件 浏览目录 Form表单上传文件 Ajax上传文件 伪造Ajax上传文件 Form表单上传文件 html 1 2 3 4 5 6 7 <h3& ...
- 第十九课 pluginlib&Nodelet
把rgb摄像头的数据转换为laser的时候使用了Nodelet. pluginlib(插件库) 在ros中有一个plugin的包,下面是一个ROS Plugin Registration的例子 上面包 ...
- Part10-C语言环境初始化-Bss段初始化lesson2
1.BSS段的作用 初始化的全局变量存放在数据段: 局部变量存放在栈中: malloc的存放在堆: 未初始化的全局变量存放在BSS段: 找到bss段的起始与结束地址,往里面添加0,便初始化好了. 打开 ...
- http请求和返回的head字段
一,http请求分请求首部字段,通用首部字段,实体首部字段.http响应包含响应首部字段,通用首部字段,实体首部字段. 二,http1.1定义了47种首部字段.1,通用首部字段:cache-contr ...