【LeetCode算法-21】Merge Two Sorted Lists
LeetCode第21题
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
翻译:
合并两个有序链表并返回一个新的链表,新链表必须由前两个链表拼接而成
思路:
把两个单链表的表头的值相互比较,较小的ListNode插入到新建的单链表中,然后更新表头,继续比较表头的值,原理和插入排序类似
步骤1:
l1:1->2->4
l2:1->3->4
l:
步骤2:
l1:1->2->4
l2:3->4
l:1->
步骤3:
l1:2->4
l2:3->4
l:1->1->
步骤4:
l1:4
l2:3->4
l:1->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) {
ListNode temp = new ListNode(-1);
ListNode l = temp; while(l1 != null && l2 != null){
if(l1.val > l2.val){
temp.next = l2;
l2 = l2.next;
}else{
temp.next = l1;
l1 = l1.next;
}
temp = temp.next;
}
temp.next = (l1!=null)?l1:l2;
return l.next;
}
}
最后返回l.next是因为l和temp两个单链表的表头地址是相同的
欢迎关注我的微信公众号:安卓圈
【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练习题21. Merge Two Sorted Lists
题目描述(easy) Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new ...
- 【一天一道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 ...
- 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 ...
随机推荐
- 业余时间折腾了个微信小程序版本的街机游戏模拟器(吾爱街机),8090后的童年回忆,欢迎大家体验
好多年没来博客园了,有段时间想玩街机游戏,发现都需要下载安装,现在小程序这么流行,是不是可以集成到小程序里(无需下载,在线玩),出于这想法,就业余时间折腾了下,分享给大家,偶尔可以回味畅玩下. 中间遇 ...
- clipse调试web项目配置调试配置——没有Server
文章:eclipse环境下如何配置tomcat(包含没有Server解决办法) 地址:https://blog.csdn.net/TimliangL/article/details/78882566
- 深度学习Keras框架笔记之Activation类使用
使用 keras.layers.core.Activation(activation) Apply an activation function tothe input.(貌似是把激活函数应用到输入数 ...
- javascript Object and new object() object --构造函数
- treegrid 折叠全部节点
$(".easyui-treegrid").treegrid({ url: '@Url.Action("GetDataDictionaryList", &quo ...
- dimensionality reduction动机---visualization(将数据可视化帮助我们更好地理解数据)
如果我们能更好地理解我们的数据,这样会对我们开发高效的机器学习算法有作用,将数据可视化(将数据画出来能更好地理解数据)出来将会对我们理解我们的数据起到很大的帮助. 高维数据如何进行显示 GDP: gr ...
- Laravel —— 特殊分页
项目中,分页经常会用到. Laravel 中也自带了分页功能. 但有些时候需要稍作修改,来满足自己的需求. 一.普通分页 1.控制器中,用 paginate() 方法. $users = DB::ta ...
- 安装cuda及之后更新环境变量的方法
安装参考:https://www.cnblogs.com/fanfzj/p/8521728.html 更新环境变量: 将 CUDA.CUPTI 和 cuDNN 安装目录添加到 %PATH% 环境变量中 ...
- 04-Flutter移动电商实战-打通底部导航栏
关于界面切换以及底栏的实现可参考之前写的一篇文章:Flutter实 ViewPager.bottomNavigationBar界面切换 1.新建4个基本dart文件 在pages目录下,我们新建下面四 ...
- WinDbg常用命令系列---.load, .loadby (Load Extension DLL)
.load, .loadby (Load Extension DLL) 简介 .load和.loadby命令将新的扩展DLL加载到调试器中. 使用形式 .load DLLName !DLLName.l ...
