Description

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的更多相关文章

  1. 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 ...

  2. 【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 ...

  3. [Leetcode][Python]21: Merge Two Sorted Lists

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 21: Merge Two Sorted Listshttps://oj.le ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 刷题21. Merge Two Sorted Lists

    一.题目说明 这个题目是21. Merge Two Sorted Lists,归并2个已排序的列表.难度是Easy! 二.我的解答 既然是简单的题目,应该一次搞定.确实1次就搞定了,但是性能太差: R ...

  9. 21.Merge Two Sorted Lists 、23. Merge k Sorted Lists

    21.Merge Two Sorted Lists 初始化一个指针作为开头,然后返回这个指针的next class Solution { public: ListNode* mergeTwoLists ...

随机推荐

  1. linux单机部署kafka(filebeat+elk组合)

    filebeat+elk组合之kafka单机部署 准备: kafka下载链接地址:http://kafka.apache.org/downloads.html 在这里下载kafka_2.12-2.10 ...

  2. ORACLE 查询所有表、外键、主键等信息

    Select   a.Owner 外键拥有者, a.Table_Name 外键表, c.Column_Name 外键列, b.Owner 主键拥有者, b.Table_Name 主键表, d.Colu ...

  3. (ACM模板)集合set

    #include<iostream> #include<cstdio> #include<set> using namespace std; int main() ...

  4. Wait and Click Element

    Wait and Click Element [Documentation] 等待元素出现并单击元素 [Arguments] ${locator} Wait Until Element Is Visi ...

  5. javascript数组赋值操作的坑

    描述:数组对象赋值,即arr1=[{},{},{}]这种数据结构的对象赋值,将arr1赋值给arr2:然后删除arr2里的元素 一.最常用的= arr2 = arr1; detect(val) { l ...

  6. CSS9:动态 REM-手机专用的自适应方案

    CSS9:动态 REM-手机专用的自适应方案 动态 REM是手机专用,是如何适配所有手机的方案,不是响应式方案,例如 : taobao.com 是专门的PC端m.taobao.com 是专门的手机端, ...

  7. Maven项目的pom.xml配置文件格式初识

    Maven项目 有pom.xml文件的项目就已经是一个maven项目了,但是还没有被maven托管,我们需要将该项目添加为maven项目 <project xmlns="http:// ...

  8. MySQL添加主键、索引

    查看索引  SHOW INDEX FROM  数据库表名 比如:SHOW INDEX FROM order_info; 添加索引 alter table 数据库add index 索引名称(数据库字段 ...

  9. 423 Locked

    TortoiseSVN提交提示423 Locked的解决办法 . 此办法是阅读官方文档(TortoiseSVN-1.6.16-zh_CN.pdf) 4.21 锁部分提供的办法: 首先选择选择要提交的文 ...

  10. Ubuntu新建用户组

    新建用户组 sudo addgroup groupname 把现有用户加入新建的用户组 sudo adduser username groupname