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 ...
随机推荐
- Nginx学习总结(一)
Nginx是目前比较主流的HTTP反向代理服务器(其企业版提供了基于TCP层的反向代理插件),对于构建大型分布式web应用,具有举足轻重的作用.简单来说,nginx有2个主要的功能:动/静态资源分离. ...
- Android作业list
作业1. 请在自己的电脑上完成Android的安装与配置,并完成Hello Android项目.上交自己与项目的合照,将照片传至QQ群中. ------------------------------ ...
- egrep 或 多个连续字符测数字
.TXT 4-6是ABC 或者 4-6是 0-9
- GSL+DevC++使用
在DEV C++中配置GSL1.8库 前面写了如何在vs2005中添加gsl,本文所所述为在dev c++中使用gsl库,由实践总结而得. 准备软件: 1.Orwell Dev C++ 5.6.2 N ...
- pycharm 进入Pythonshell脚本调试
- python学习笔记(一)python简介和基础
1.什么是python? python是一种面向对象的,解释型的计算机语言,它的特点是语法简介,优雅,简单易学.1989年诞生,Guido(龟叔)开发. 编译型语言:代码在编译之后,编译成2进制的文件 ...
- LDD3 第11章 内核的数据类型
考虑到可移植性的问题,现代版本的Linux内核的可移植性是非常好的. 在把x86上的代码移植到新的体系架构上时,内核开发人员遇到的若干问题都和不正确的数据类型有关.坚持使用严格的数据类型,并且使用-W ...
- HDU 6053 TrickGCD —— 2017 Multi-University Training 2
TrickGCD Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- delphi中如何将一整个文件读入内存
来源 https://bbs.csdn.net/topics/390985048 分配一块大内存吧,要是一下申请不了64M那么大,就多申请几块小的,用个链表连起来.用FileStream类的方法读取文 ...
- Bugku | 数字验证正则绕过
语法: int preg_match_all (字符串$ pattern ,字符串$ subject [,数组和$ matches [,整数$ flags = PREG_PATTERN_ORDER [ ...