【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.
思路:比较每个列表的第一个元素。 合并小的添加到列表中。 最后,当其中一个是空的,只需将它附加到合并后的列表,因为它已经排序。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode head=new ListNode(-1);
ListNode addNode=head;
while(l1!=null&&l2!=null){
if(l1.val<l2.val){
addNode.next=l1;
l1=l1.next;
}else{
addNode.next=l2;
l2=l2.next;
}
addNode=addNode.next;
}
if(l1==null) addNode.next=l2;
if(l2==null) addNode.next=l1;
return head.next;
}
}
【LeetCode】21. Merge Two Sorted Lists的更多相关文章
- 【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...
- 【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】23. Merge k Sorted Lists 合并K个升序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,链表,单链表,题解,leetcode, 力扣,Py ...
- 【一天一道LeetCode】#21. Merge Two Sorted Lists
一天一道LeetCode系列 (一)题目 Merge two sorted linked lists and return it as a new list. The new list should ...
- 【LeetCode】023. Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题 ...
- 【LeetCode】021. 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】23. Merge k Sorted Lists
合并k个已合并链表. 思路:先把链表两两合并,直到合并至只有一个链表 /** * Definition for singly-linked list. * struct ListNode { * in ...
- [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 ...
随机推荐
- lwip:网络数据包读取和解析过程
1. 程序的某处(poll or interrupt)在有数据可读时调用ethernetif_input,该函数依次调用以下函数: 1.1 low_level_input(),将网络数据读入内存: 1 ...
- Redis主从同步介绍
Redis主从同步命令和配置项 启动主从复制:master无需任何操作,slave中使用以下任意一种开启复制功能 (1).通过配置文件启动主从复制: 在redis.conf中加入"slave ...
- C语言每日一题之No.4
这几天老大也没安排我什么项目,于是想正好趁着空补C.当然,是利用晚上加班时间,白天正常上班时间还是学习公司的平台. 今儿个突然弱弱的感觉到在公司补C是件很低级的事情,哪怕是在加班时间都会被喷,因为大家 ...
- 【转】一道SQL SERVER DateTime的试题
学习过上一篇SQL SERVER DateTime精度的文章后.再来做一道题. IF ('2011-07-31 00:00:00.000' BETWEEN '2011-07-01' and '2011 ...
- 【SQL Server】系统学习之二:索引优化
页大小8192个字节,行限制为8060字节(大型对象除外). 包含varchar nvarchar varbinary sql_variant(8012,object类型) clr 的行,如果行大小超 ...
- [Android Studio 1A] – 插件
关于android studio插件的使用,一直没有好的机会来研究,最近项目开发中需要统计项目的代码行数,所以就需要一个代码统计的插件.我选择的是 Statistic,下载地址是:https://pl ...
- OAF_EO系列2 - Validation数据校验验证机制(概念)
2014-06-12 Created By BaoXinjian
- Going Home (hdu 1533 最小费用流)
集训的图论都快结束了,我才看懂了最小费用流,惭愧啊. = = 但是今天机械键盘到了,有弄好了自行车,好高兴\(^o^)/~ 其实也不是看懂,就会套个模板而已.... 这题最重要的就是一个: 多组输入一 ...
- POJ 3162 Walking Race 树形DP+线段树
给出一棵树,编号为1~n,给出数m 漂亮mm连续n天锻炼身体,每天会以节点i为起点,走到离i最远距离的节点 走了n天之后,mm想到知道自己这n天的锻炼效果 于是mm把这n天每一天走的距离记录在一起,成 ...
- Javascript函数声明与函数表达式
在定义函数时,我们一般使用下面这两种方法: 使用函数声明定义: function sum (a, b) { return a + b; } 使用函数表达式定义: var sum = function ...