LeetCode Linked List Easy 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.
Example:
Input: ->->, ->->
Output: ->->->->->
问题描述,将两个排序的链表归并
代码:
public ListNode MergeTwoLists(ListNode l1, ListNode l2) {
ListNode ln1 = l1;
ListNode ln2 = l2;
ListNode res =new ListNode();
ListNode temp = res;
while(ln1 != null && ln2 != null){
if(ln1.val < ln2.val){
temp.next = ln1;
ln1 =ln1.next;
temp = temp.next;
}else{
temp.next = ln2;
ln2 =ln2.next;
temp = temp.next;
}
}
while(ln1 != null){
temp.next = ln1;
ln1 =ln1.next;
temp = temp.next;
}
while(ln2 != null){
temp.next = ln2;
ln2 =ln2.next;
temp = temp.next;
}
return res.next;
LeetCode Linked List Easy 21. Merge Two Sorted Lists的更多相关文章
- 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】【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 t ...
- [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【easy】
21. Merge Two Sorted Lists[easy] Merge two sorted linked lists and return it as a new list. The new ...
- 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(照搬大神做法)
1.题目 21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new l ...
- 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
一.题目说明 这个题目是21. Merge Two Sorted Lists,归并2个已排序的列表.难度是Easy! 二.我的解答 既然是简单的题目,应该一次搞定.确实1次就搞定了,但是性能太差: R ...
- 21.Merge Two Sorted Lists 、23. Merge k Sorted Lists
21.Merge Two Sorted Lists 初始化一个指针作为开头,然后返回这个指针的next class Solution { public: ListNode* mergeTwoLists ...
随机推荐
- 基于ZYNQ XC7Z045 FFG 900的高性能计算模块
一.板卡概述 本板卡基于Xilinx公司的FPGA XC7Z045 FFG 9000 芯片, 该平台为设计和验证应用程序提供了一个完整的开发平台.该平台使设计师能够更加简单进行高性能的原型设计,并且通 ...
- Git--03 git分支
目录 Git分支 1.新建testing分支 2.合并分支 3.合并冲突 4.删除分支 Git标签使用 1.查看标签 02.删除标签 Git分支 分支即是平行空间,假设你在为某个手机系统研发拍照功 ...
- springBoot相关(一)
2.0新特性: 编程语言: Java8+.Kotlin 底层框架:Spring Framwork 5.0.x 全新特性: web Flux web Flux: 函数编程:java 8 Lambda 响 ...
- Linux中的uniq命令(去掉重复项,输出重复项)
ls /bin /usr/bin | sort | uniq | less 上面这条命令的实际效果是: 获得 ls /bin /usr/bin 的 output 将上述 output 进行 sort ...
- service-web
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://w ...
- SQL Server 创建表
SQL Server 创建表 我们在上一节中完成了数据库的创建,在本节,我们要往这个新的数据库中加入点数据,要想将数据添加到数据库,我们就必须在数据库中添加一个表,接下来来看看具体的操作. 我们的数据 ...
- python3运行报错:TypeError: Object of type 'type' is not JSON serializable解决方法(详细)
返回结果先转成str 字符创
- angular项目引用第三方公共js文件
由于项目需要,领导要求在正在开发的angular项目中,引入公共js,以便进行统计计算. 于是便各种找度娘,网上有好多引用jquery插件的例子,于是便按照步骤对自身项目进行了改造,先记录一下: st ...
- (转载)解决vmware上安装ubuntu不能联网的问题
在vmware中安装Ubuntu之后,我们希望基本的功能如上网.传输文件等功能都是可用的,但是经常遇到不能上网的情况.使用笔记本时,我们经常希望能通过无线网卡上网,但是在做嵌入式开发时,我们还希望虚拟 ...
- Linux后台执行脚本 &与nohup
Linux后台执行脚本的方式: 0.脚本代码 [root@VM_1_3_centos apps]# cat test.php <?php sleep(5); echo "hello w ...