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 ...
随机推荐
- sql for loop
--step1 disable constraint begin for i in (select uc.constraint_name, uc.table_name from user_constr ...
- mysql授予权限
grant show databases on *.* to 'asg'@'%'; grant select on *.* to 'asg'@'%'; grant show view on *.* t ...
- orcad原理图与PSPICE模型库名称
Vendor PSpice Model Description Advanced Linear Devices adv_lin.lib Library of op-amps Advanced Line ...
- instanceof是Java的一个二元操作符(运算符)
instanceof是Java的一个二元操作符(运算符),也是Java的保留关键字.它的作用是判断其左边对象是否为其右边类的实例,返回的是boolean类型的数据.用它来判断某个对象是否是某个Clas ...
- Codeforces 499C:Crazy Town(计算几何)
题目链接 给出点A(x1,y1),B(x2,y2),和n条直线(ai,bi,ci,aix + biy + ci = 0),求A到B穿过多少条直线 枚举每条直线判断A.B是否在该直线两侧即可 #incl ...
- MongoDB笔记【2】——基本概念和基本指令
- 基本概念 数据库(database) 集合(collection) 文档(document) - 在MongoDB中,数据库和集合都不需要手动创建,当我们创建文档时,如果文档所在的集合或数据库不存 ...
- socket | netcat 模拟
#!/opt/local/bin/python2.7 #coding=utf-8 ''' 取代netcat 两台主机中其中一台控制另一台 得到北控方的shell ''' import sys impo ...
- [CSP-S模拟测试]:Tourist Attractions(简单图论+bitset)
题目描述 在美丽的比特镇一共有$n$个景区,编号依次为$1$到$n$,它们之间通过若干条双向道路连接.$Byteasar$慕名来到了比特镇旅游,不过由于昂贵的门票费,他只能负担起$4$个景区的门票费. ...
- (转)堆和栈的概念和区别 HeapOutOfMemory和StackOverflow解释
转:https://blog.csdn.net/pt666/article/details/70876410 https://blog.csdn.net/guohan_solft/article/de ...
- 用 Flask 来写个轻博客 (20) — 实现注册表单与应用 reCAPTCHA 来实现验证码
Blog 项目源码:https://github.com/JmilkFan/JmilkFan-s-Blog 目录 目录 前文列表 reCAPTCHA 应用 reCAPTCHA 前文列表 用 Flask ...