LeetCode第21题

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: 1->2->4, 1->3->4

Output: 1->1->2->3->4->4

翻译

合并两个有序链表并返回一个新的链表,新链表必须由前两个链表拼接而成

思路:

把两个单链表的表头的值相互比较,较小的ListNode插入到新建的单链表中,然后更新表头,继续比较表头的值,原理和插入排序类似

步骤1:

l1:1->2->4

l2:1->3->4

l:

步骤2:

l1:1->2->4

l2:3->4

l:1->

步骤3:

l1:2->4

l2:3->4

l:1->1->

步骤4:

l1:4

l2:3->4

l:1->1->2->

...

最后两个链表肯定有一个是没比较完的,直接加在新建的链表最后就行了

代码:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode temp = new ListNode(-1);
ListNode l = temp; while(l1 != null && l2 != null){
if(l1.val > l2.val){
temp.next = l2;
l2 = l2.next;
}else{
temp.next = l1;
l1 = l1.next;
}
temp = temp.next;
}
temp.next = (l1!=null)?l1:l2;
return l.next;
}
}
最后返回l.next是因为l和temp两个单链表的表头地址是相同的
欢迎关注我的微信公众号:安卓圈

【LeetCode算法-21】Merge Two Sorted Lists的更多相关文章

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

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

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

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

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

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

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

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

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

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

  8. 【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 spli ...

  9. LeetCode:21. Merge Two Sorted Lists(Easy)

    1. 原题链接 https://leetcode.com/problems/merge-two-sorted-lists/description/ 2. 题目要求 给出两个已经从小到大排序的链表ls1 ...

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

随机推荐

  1. 嵌入式Linux框架的理解

    从事嵌入式linux工作也几年了,如果算上大学期间的自学,那么也算是个工程师了.期间写过底层bootloader.内核的驱动和上层应用程序.对于芯片内部的模块也在大学时候用fpga的verilog玩过 ...

  2. jquery 子元素筛选选择器

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content ...

  3. ACAG 0x01-4 最短Hamilton路径

    ACAG 0x01-4 最短Hamilton路径 论为什么书上标程跑不过这道题-- 首先,这道题与今年CSP-S2的D1T3有着异曲同工之妙,那就是--都有$O(n!)$的做法!(大雾) 这道题的正解 ...

  4. CentOS7下的AIDE入侵检测配置

    一.AIDE的概念 AIDE:Advanced Intrusion Detection Environment,是一款入侵检测工具,主要用途是检查文档的完整性.AIDE在本地构造了一个基准的数据库,一 ...

  5. Qualification Rounds(Codeforces Round #438 by Sberbank and Barcelona Bootcamp (Div. 1 + Div. 2 combined)+状态压缩)

    题目链接 传送门 题意 现总共有\(n\)个题目\(k\)支参赛队伍,已知每个题目各队伍是否会写,现问你能否从题目中选出一个子序列使得每支队伍最多只会写一半的题目. 思路 对于每个题目我们用二进制压缩 ...

  6. app开发-1

    一.了解HBuilder HBuilder内封装了大量的书籍,极大方便了使用 官方文档: http://dev.dcloud.net.cn/mui/ui/ 关于布局: mhead  表头.mbody ...

  7. steam相关插件

    批量激活key:https://greasyfork.org/zh-CN/scripts/32718-steamredeemkeys 批量卖卡:https://github.com/Nuklon/St ...

  8. 小程序登录及AppSecret(小程序密钥)

    在授权开发以后,需要提交小程序密钥,有小程序密钥第三方才有能力获取用户的一些信息,提供一些能力! 平台分别提供多种方式实现微信登录: 1. 调用wx.login接口,静默获取openid 适用场景:无 ...

  9. js判断是否第一次访问跳转

    今天分享一套关于Js劫持代码,进行判断第一次访问进行跳转,仅供大家参考学习! 未加密: if (c.indexOf('isfirstvisited=false') != -1) { } else { ...

  10. 解决Mac外接显示器字体模糊的问题

    Mac外接显示器时,除非接的是Apple自家的显示器“ACD”,不然一般会遇到字体模糊发虚的问题.在终端中执行命令: defaults -currentHost write -globalDomain ...