题目描述:

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.

解题分析:

再基础不过的题了,直接看代码吧^-^

具体代码:

 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public static ListNode mergeTwoLists(ListNode head1, ListNode head2) {
if(head1==null)
return head2;
if(head2==null)
return head1;
ListNode head=null;
ListNode current=null;
if(head1.val<=head2.val){
head=head1;
head1=head1.next;
head.next=null;
}
else{
head=head2;
head2=head2.next;
head.next=null;
}
current=head;
while(head1!=null&&head2!=null){
if(head1.val<=head2.val){
current.next=head1;
current=current.next;
head1=head1.next;
current.next=null;
}
else{
current.next=head2;
current=current.next;
head2=head2.next;
current.next=null;
}
}
if(head1!=null){
current.next=head1;
}
if(head2!=null){
current.next=head2;
}
return head;
}
}

【leetcode】 21. Merge Two Sorted Lists的更多相关文章

  1. 【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...

  2. 【LeetCode】21. Merge Two Sorted Lists

    题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...

  3. 【LeetCode】23. Merge k Sorted Lists 合并K个升序链表

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,链表,单链表,题解,leetcode, 力扣,Py ...

  4. 【一天一道LeetCode】#21. Merge Two Sorted Lists

    一天一道LeetCode系列 (一)题目 Merge two sorted linked lists and return it as a new list. The new list should ...

  5. 【LeetCode】023. Merge k Sorted Lists

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题 ...

  6. 【LeetCode】021. 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 ...

  7. 【LeetCode】23. Merge k Sorted Lists

    合并k个已合并链表. 思路:先把链表两两合并,直到合并至只有一个链表 /** * Definition for singly-linked list. * struct ListNode { * in ...

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

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

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

随机推荐

  1. myeclipse注册码,可以用到2016年

    myeclipse注册码,可以用到2016年 xiangyang kLR8ZF-655954-61677756068297221

  2. UndertowServer+SpringMVC+Thymeleaf模板引擎构建轻量级的web项目

    这两周需要写一个页面来请求另一个服务中的接口,服务器采用了超轻量级的undertow,模板引擎采用的是Thymeleaf,在寻找页面资源位置这个地方难住了我.下面分享一下,这方面的代码. Spring ...

  3. HTML入门(一)

    ---恢复内容开始--- HTML 一 .HTML介绍 1. 什么是HTML? 超文本标记语言: 超文本: 比普通文本更强大 标记语言: 使用一组标签对内容进行描述的一门语言,它不是编程语言! 2. ...

  4. c# 重载运算符(ovveride operator)踩坑记,关于null比对

    场景描述: 需要比对两个版本的对应对象是否完全一致(每个属性值一致),不一致的导出报表颜色标识,以便提醒后续使用报表人员. 实现思路: 对象重载ToString方法,另实现一比对基类(为了通用)重载= ...

  5. 【leetcode 简单】第十题 实现strStr()

    实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在,则返 ...

  6. solr笔记之安装部署到tomcat

    1. 下载 solr 去官网下载,下载的时候选清华的镜像源,这个页面:https://mirrors.tuna.tsinghua.edu.cn/apache/lucene/solr/7.1.0/ 在/ ...

  7. 【文件上传】jquery之ajaxfileupload异步上传插件

    来自:http://www.blogjava.net/sxyx2008/archive/2010/11/02/336826.html 由于项目需求,在处理文件上传时需要使用到文件的异步上传.这里使用J ...

  8. attachEvent 中this指向

    IE中使用的事件绑定函数与Web标准的不同,而且this指向也不一样,Web标签中的this指向与传统事件绑定中的this一样,是当前目标,但是IE中事件绑定函数中this指向,通过使用call或ap ...

  9. EasyUi组合条件分页查询

    1.引入css与js文件 <link rel="stylesheet" type="text/css" href="themes/default ...

  10. JSON与JS的区别以及转换

    JSON是什么?(JSON和JavaScript对象有什么区别?)如何把JS对象转化为JSON字符串,又如何把JSON字符串转化为JavaScript对象? JSON (JavaScript Obje ...