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. 8VC Venture Cup 2017 - Elimination Round - A

    题目链接:http://codeforces.com/contest/755/problem/A 题意:给定一个正整数N,问你是否存在一个数M使得N*M+1不是素数(M的范围在[1,1000]). 思 ...

  2. 《深入学习Redis(2):持久化》笔记

    参考 https://www.cnblogs.com/kismetv/p/9137897.html 一.高可用概述 提供正常服务:主从分离,快速容灾技术,数据容量的扩展.数据安全不会丢失.    持久 ...

  3. redis还要做

    RedisTemplate对各种数据类型的操作记录. Redis深度历险:核心原理和应用实践 https://www.cnblogs.com/kismetv/p/8654978.html

  4. 如何去除inline-block的默认间距

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title&g ...

  5. java Random随机生成一个数

    package java05; import java.util.Random; /* Random随机生成一个数字 1.导包: import java.util.Random; 2.创建 Rando ...

  6. HTML5 canvas绘制图片

    demo.js window.onload=function() { createcanvas(); drawImage(); } function createcanvas() { var CANV ...

  7. vue中引入了sass,又引入cssnano报错

    "cssnano": { // preset: "advanced", autoprefixer: false, "postcss-zindex&qu ...

  8. py2 一键转换成py3

    找到python自带的2to3-3.7 文件,在终端里输入 python 2to3-3.7 -w /Users/v_fanjiexiong/PycharmProjects/HugeGraph_fan/ ...

  9. 设置Oracle PL/SQL时间显示格式NLS_TIMESTAMP_FORMAT

    Oracle中TIMESTAMP时间的显示格式   Oracle数据库的时间字段我们通常是使用timestamp 格式,在未做设置前, 查询出来的数据类似于“27-1月 -08 12.04.35.87 ...

  10. JSP和selevt 生命周期详解(JSP的生命周期和select很像,jsp底层就是一个selevt)

    JSP: JSP的生命周期指从创建到销毁的整个过程.分为以下几个阶段: 1:编译阶段:servlet引擎编译servlet源文件,生成servlet类.当浏览器请求JSP页面时,JSP引擎会首先去检查 ...