1、题目

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 together the nodes of the first two lists.

Example:

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

2、我的解答

 # -*- coding: utf-8 -*-
# @Time : 2020/2/1 18:30
# @Author : SmartCat0929
# @Email : 1027699719@qq.com
# @Link : https://github.com/SmartCat0929
# @Site :
# @File : 21.Merge Two Sorted Lists.py # Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
head = l3 = ListNode(0)
while(l1 is not None and l2 is not None):
if l1.val <=l2.val:
l3.next = ListNode(l1.val)
l3 = l3.next
l1 = l1.next
else:
l3.next = ListNode(l2.val)
l3 = l3.next
l2 = l2.next
while l1:
l3.next = ListNode(l1.val)
l3 = l3.next
l1 = l1.next
while l2:
l3.next = ListNode(l2.val)
l3 = l3.next
l2 = l2.next
return head.next h1 = ListNode(1)
h1.next = ListNode(2)
h1.next.next = ListNode(4) h2 = ListNode(1)
h2.next = ListNode(3)
h2.next.next = ListNode(4) h = Solution().mergeTwoLists(h1, h2)
while h is not None:
print(h.val, end="->")
h = h.next

leetCode练题——21. Merge Two Sorted Lists(照搬大神做法)的更多相关文章

  1. 刷题21. Merge Two Sorted Lists

    一.题目说明 这个题目是21. Merge Two Sorted Lists,归并2个已排序的列表.难度是Easy! 二.我的解答 既然是简单的题目,应该一次搞定.确实1次就搞定了,但是性能太差: R ...

  2. LeetCode记录之21——Merge Two Sorted Lists

    算法和数据结构这东西,真的是需要常用常练.这道看似简单的链表合并题,难了我好几个小时,最后还是上网搜索了一种不错算法.后期复习完链表的知识我会将我自己的实现代理贴上. 这个算法巧就巧在用了递归的思想, ...

  3. [LeetCode&Python] Problem 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❤python】21. Merge Two Sorted Lists

    #-*- coding: UTF-8 -*- # Definition for singly-linked list.# class ListNode(object):#     def __init ...

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

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

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

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

  8. 21.Merge Two Sorted Lists 、23. Merge k Sorted Lists

    21.Merge Two Sorted Lists 初始化一个指针作为开头,然后返回这个指针的next class Solution { public: ListNode* mergeTwoLists ...

  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. 001.Django_Model.整理

    Django001_Model.整理 Model表设计 数据定义数据存储,输出 a.定义表(信息 =字段) + 定义表关系 + (定义/限制)数据 b.通过orm等方法来,定义method来编辑原始数 ...

  2. 路飞-pip源

    pip安装源 介绍 """ 1.采用国内源,加速下载模块的速度 2.常用pip源: -- 豆瓣:https://pypi.douban.com/simple -- 阿里: ...

  3. vue生命周期钩子函数详解

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/qq_35585701/article/ ...

  4. if (flag)

    var flag = false;//暂停标记if (flag){ alert("aa")}else { alert("bb")}bb if (flag) = ...

  5. Homebrew安装和Mac使用

    软件安装 1.Homebrew安装   ruby环境: curl -sSL https://get.rvm.io | bash -s stable   官网方式: /usr/bin/ruby -e & ...

  6. [lua]紫猫lua教程-命令宝典-L1-01-05. if判断结构

    L1[if]01. 简单的if判断结构 没什么说得 if得基本结构如下 xxx= ) then testlib.traceprint("1-100") ) then testlib ...

  7. 每天进步一点点------入门视频采集与处理(BT656简介)

    凡是做模拟信号采集的,很少不涉及BT.656标准的,因为常见的模拟视频信号采集芯片都支持输出BT.656的数字信号,那么,BT.656到底是何种格式呢?      本文将主要介绍 标准的 8bit B ...

  8. 清除定时器 和 vue 中遇到的定时器setTimeout & setInterval问题

    2019-03更新 找到了更简单的方法,以setinterval为例,各位自行参考 mounted() { const that = this const timer = setInterval(fu ...

  9. Java内存模型(JMM)那些事

    本文是库存文章,去年年底学习了慕课网的并发编程课程,今年年初看完了<深入理解Java虚拟机>这本书,但是很多内容忘得差不多了,打算写写博客回忆一下那些忘在脑后的知识点. 温故而知新 更多J ...

  10. 图片上传至/target/upload目录下后,通过ip:port/upload/无法访问

    做以下配置即可 @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { try { registry ...