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. FreeRTOS学习笔记5:队列

    为通信而准备 1.任务存储存储缓冲机制:先进先出 FIFO 后进先出 LIFO (ucOS消息队列采用的是引用传递,传递的都是指针.采用引用的话,内容必须一致保持可见性,即消息内容必须有效.1.不能传 ...

  2. 2019 徐州网络赛 center

    题意:n个点,求最小加上几个点让所有点关于一个点(不需要是点集里面的点)中心对称 题解:双重循环枚举,把中点记录一下,结果是n-最大的中点 #include <bits/stdc++.h> ...

  3. Httpclient 工具类(get,put)

    package com.googosoft.until; import java.io.IOException; import org.apache.http.HttpEntity; import o ...

  4. RAID 0实验:mdadm

    *独立冗余磁盘阵列---RAID0* RAID0: 把多块物理硬盘设备(至少两块)通过硬件或软件的方式串联在一起, 组成 一个大的卷组,并将数据依次写入到各个物理硬盘中.任意一块 硬盘发生故障将导致整 ...

  5. 《爬虫学习》(一)(HTTP协议)

    Http请求: 1.在浏览器中发送一个http请求的过程: 2.url详解: URL是Uniform Resource Locator的简写,统一资源定位符. 一个URL由以下几部分组成 scheme ...

  6. mybatis中条件查询大于等于和小于等于写法

    原符号 < <= > >= & ' "替换符号 < <= > >= & &apos; " createDat ...

  7. zookeeper使用及安装

    1.安装查看:http://ifeve.com/zookeeper-talk-quick-start/ 2.使用: Zookeeper是一个高性能的分布式应用协调服务的框架.Zookeeper=Zoo ...

  8. 树莓派安装ubuntu_meta并配置开发环境

    1.烧录系统 首先准备好我们要烧录的ubuntu_meta系统,可以在树莓派官网中下载https://www.raspberrypi.org/downloads/ 这里我们选择 Raspberry P ...

  9. RTU license

    Right to Use (RTU) licensing is a model in which licenses are not tied to a unique device identifier ...

  10. codeforces-Three Friends

      Three Friends Three friends are going to meet each other. Initially, the first friend stays at the ...