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 ...
随机推荐
- 64-基于TMS320C6455、XC5VSX95T 的6U CPCI无线通信处理平台
基于TMS320C6455.XC5VSX95T 的6U CPCI无线通信处理平台 1. 板卡概述 本板卡由我公司自主研发,基于CPCI架构,符合PICMG2.0 D3.0标准,包含双TI TMS3 ...
- vue,一路走来(6)--微信支付
微信支付 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7&index=6 分享一下vue实现微信支付.在微信浏览器里面 ...
- Go 使用 append 向切片增加元素
1.// 创建一个整型切片 // 其长度和容量都是 5 个元素 slice := []int{10, 20, 30, 40, 50} // 创建一个新切片 // 其长度为 2 个元素,容量为 4 个元 ...
- 异常 Cannot resolve class or package
spring boot yml配置异常Cannot resolve class or package 是因为mvaen设置 pom.xml的文件配置如上,scope 范围指定为runtime,runt ...
- java 比较两个日期大小(2) 用before(), after()
调试代码,我就不整理了,记下after() before() 觉得这张图好美,从人家的博客上截的,找不到链接了
- Unparseable date: "Mon Aug 15 11:24:39 CST 2016",时间格式转换异常
String datestr= "Mon Aug 15 11:24:39 CST 2016";//Date的默认格式显示 Date date=new SimpleDateForma ...
- Yii2.0基础框架
前言:最近在用php写一个项目的接口,所以需要学习一下Yii的框架,也在这里记录一下. 整体结构 ssets文件夹:assets的作用是方便模块化,插件化的,一般来说出于安全原因不允许通过url访问p ...
- Lock之ReentrantLock及实现生产者消费者和死锁
Lock是顶层接口,它的实现逻辑并未用到synchronized,而是利用了volatile的可见性.ReentrantLock对了Lock接口的实现主要依赖了Sync,而Sync继承了 Abstra ...
- flutter Container组件和Text组件
在开始之前,我们先写一个最简单的入口文件: 后面,都是在这个结构的基础上面完成的. 由于Container组件和Text组件都是写在body里面的,所以下面,先将body抽离成一个组件的形式. ...
- 如何写一个bat文件,让他去执行某一个地方的bat文件
新建一个bat文件,里面编写如下内容:@echo offcall 你bat文件的路径\startup.bat pause --------------------------------------- ...