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

题目地址:https://leetcode.com/problems/merge-two-sorted-lists/

题目描述

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:

  1. Input: 1->2->4, 1->3->4
  2. Output: 1->1->2->3->4->4

题目大意

把两个有序列表合并成一个有序列表。

解题方法

迭代

直接自己定义一个链表的头,循环两个链表,每次把两个链表头部较小的那个节点放到结尾。最后不要忘了如果链表有剩余,应该拼接起来。

Python解法

这个题有个升级版本23. Merge k Sorted Lists,比这题有意思,可以看看。

  1. # Definition for singly-linked list.
  2. # class ListNode(object):
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.next = None
  6. class Solution(object):
  7. def mergeTwoLists(self, l1, l2):
  8. """
  9. :type l1: ListNode
  10. :type l2: ListNode
  11. :rtype: ListNode
  12. """
  13. head = ListNode(0)
  14. move = head
  15. if not l1: return l2
  16. if not l2: return l1
  17. while l1 and l2:
  18. if l1.val < l2.val:
  19. move.next = l1
  20. l1 = l1.next
  21. else:
  22. move.next = l2
  23. l2 = l2.next
  24. move = move.next
  25. move.next = l1 if l1 else l2
  26. return head.next

C++解法

C++注意全部是指针操作,要用指针操作符。

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode(int x) : val(x), next(NULL) {}
  7. * };
  8. */
  9. class Solution {
  10. public:
  11. ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
  12. if (!l1) return l2;
  13. if (!l2) return l1;
  14. ListNode* dummy = new ListNode(0);
  15. ListNode* cur = dummy;
  16. while (l1 && l2) {
  17. if (l1->val < l2->val) {
  18. cur->next = l1;
  19. l1 = l1->next;
  20. } else {
  21. cur->next = l2;
  22. l2 = l2->next;
  23. }
  24. cur = cur->next;
  25. }
  26. if (l1) cur->next = l1;
  27. else cur->next = l2;
  28. return dummy->next;
  29. }
  30. };

Java解法

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode(int x) { liebiaoval = x; }
  7. * }
  8. */
  9. public class Solution {
  10. public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
  11. ListNode head=new ListNode(0);
  12. ListNode move=head;
  13. while(l1!=null && l2!=null){
  14. if(l1.val<=l2.val){
  15. move.next=l1;
  16. l1=l1.next;
  17. }else{
  18. move.next=l2;
  19. l2=l2.next;
  20. }
  21. move=move.next;
  22. }
  23. if(l1!=null){
  24. move.next=l1;
  25. }else{
  26. move.next=l2;
  27. }
  28. return head.next;
  29. }
  30. }

AC:1ms

递归

递归很好写了,这个函数是把两个有序的merge,所以找到当前的小的节点,然后把后面的继续merge就好了。

  1. # Definition for singly-linked list.
  2. # class ListNode:
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.next = None
  6. class Solution:
  7. def mergeTwoLists(self, l1, l2):
  8. """
  9. :type l1: ListNode
  10. :type l2: ListNode
  11. :rtype: ListNode
  12. """
  13. if not l1 and not l2:
  14. return None
  15. elif not l1:
  16. return l2
  17. elif not l2:
  18. return l1
  19. if l1.val <= l2.val:
  20. node = l1
  21. node.next = self.mergeTwoLists(l1.next, l2)
  22. else:
  23. node = l2
  24. node.next= self.mergeTwoLists(l1, l2.next)
  25. return node

日期

2016/5/1 19:35:14
2018 年 3 月 11 日
2018 年 11 月 18 日 —— 出去玩了一天,腿都要废了
2019 年 1 月 11 日 —— 小光棍节?

【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表的更多相关文章

  1. leetcode 21 Merge Two Sorted Lists 合并两个有序链表

    描述: 合并两个有序链表. 解决: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if (!l1) return l2; if (!l2) ...

  2. LeetCode 21. Merge Two Sorted Lists合并两个有序链表 (C++)

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

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

  4. 【LeetCode】Merge Two Sorted Lists(合并两个有序链表)

    这道题是LeetCode里的第21道题. 题目描述: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1-&g ...

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

  6. [LeetCode] 23. Merge k Sorted Lists 合并k个有序链表

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

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

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

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

随机推荐

  1. 30-Container With Most Water-Leetcode

    Given n non-negative integers a1, a2, -, an, where each represents a point at coordinate (i, ai). n ...

  2. [Emlog主题] Monkey V3.0 优化修改

    原作者博客:https://blog.dyboy.cn/ Monkey V3.0 优化修改版 修改说明: 背景颜色修改(按个人喜好可自行修改,仿PCQQ午夜巴黎皮肤) 搜索框按钮样式优化,不那么突兀了 ...

  3. day25 组合和内置函数

    day25 组合和内置函数 一.组合 # 解决类与类之间代码冗余问题: 1. 继承 2. 组合 组合:一个对象拥有一个属性, 属性的值必须是另外一个对象 继承满足的是:什么是什么的关系 # is-a ...

  4. Oracle—merge into语法

    oracle的merge into语法,在这种情况下: 基于某些字段,存在就更新,不存在就插入: 不需要先去判断一下记录是否存在,直接使用merge into merge into 语法: MERGE ...

  5. Oracle中建表及表操作

    一.创建表 Oracle中的建表语句:create table 表名( 字段名1 数据类型 列属性,字段名2 数据类型 列属性,...... ) 如:创建表OA_DM.DM_GY_USER https ...

  6. android 下动态获取控件的id

    有时候我们需要动态的取得一个一个控件的id,然后进行操作,经过在网上查找,找到了一下方法getResources().getIdentifier("textView01", &qu ...

  7. zabbix之监控面试

    先用shell脚本把值取出来,然后重启agent,在server端用zabbix-get命令测试一下,看能不能通过userparameter指定的可以将值取出来,如果没问题,在在网页创建模板,加监控项 ...

  8. linux环境下安装jdk,tomcat

    一.安装tomcat 1.使用docker安装(你得linux服务器上已经安装了docker) 1)执行命令: docker search tomcat; 2)选择第一个镜像进行下载,执行命令:doc ...

  9. 【Linux】【Services】【Package】Basic

    Linux程序包管理           概述         API:Application Program Interface         ABI:Application Binary Int ...

  10. Maven项目打包成war包并启动war包运行

    1 项目打包 1.1 右键点击所需要打包的项目,点击如图所示 Maven clean,这里 Maven 会清除掉之前对这个项目所有的打包信息. 1.2进行完 Maven clean 操作后,在ecli ...