题目描述:

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. OpenCV---图像梯度

    图像梯度 推文:[OpenCV入门教程之十二]OpenCV边缘检测:Canny算子,Sobel算子,Laplace算子,Scharr滤波器合辑 图像梯度可以把图像看成二维离散函数,图像梯度其实就是这个 ...

  2. poi对excel的操作(二)

    二.进阶的对象 1.CellType 枚举类 单元格类型 CellType.STRING---文本型 等 如何是指单元格类型:cell.setCellType(CellType.STRING) 2.C ...

  3. SPOJ DQUERY 离线树状数组+离散化

    LINK 题意:给出$(n <= 30000)$个数,$q <= 2e5$个查询,每个查询要求给出$[l,r]$内不同元素的个数 思路:这题可用主席树查询历史版本的方法做,感觉这个比较容易 ...

  4. Parencodings(模拟)

    ZOJ Problem Set - 1016 Parencodings Time Limit: 2 Seconds      Memory Limit: 65536 KB Let S = s1 s2 ...

  5. .net core 中 identity server 4 之术语

    id4的职责: 保护你的资源 通过本地 账户库(Account Store)或者外部身份提供其 认证用户 提供Session管理以及SSO 管理和认证客户端 发行身份及访问Token给客户端 验证To ...

  6. Isomorphic JavaScript: The Future of Web Apps(译)

                                                                                                 Isomorp ...

  7. R0—New packages for reading data into R — fast

    小伙伴儿们有福啦,2015年4月10日,Hadley Wickham大牛(开发了著名的ggplots包和plyr包等)和RStudio小组又出新作啦,新作品readr包和readxl包分别用于R读取t ...

  8. LOW逼三人组(二)----选择排序算法

    选择排序思路: 算法程序: def cal_time(func): # 装饰器 ,用来检测算法所执行的时间 def wrapper(*args,**kwargs): t1=time.time() re ...

  9. 数组中的each 和 jquery 中的 each

    数组的实例上都有一个叫做 forEach 的方法,这个方法定义在 Array.prototype 上,所以数组的所有实例都可以使用 forEach 这个方法. forEach 方法的语法结构如下: v ...

  10. vsftpd 安装配置详细教程

    linux下ftp软件不少,大致特点:<br /> wu-ftp:比较老牌,但针对它的攻击比较多,设置比较麻烦,但功能比较强大.<br /> vsftpd:功能强大,配置也比较 ...