leetCode练题——21. Merge Two Sorted Lists(照搬大神做法)
1、题目
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(照搬大神做法)的更多相关文章
- 刷题21. Merge Two Sorted Lists
一.题目说明 这个题目是21. Merge Two Sorted Lists,归并2个已排序的列表.难度是Easy! 二.我的解答 既然是简单的题目,应该一次搞定.确实1次就搞定了,但是性能太差: R ...
- LeetCode记录之21——Merge Two Sorted Lists
算法和数据结构这东西,真的是需要常用常练.这道看似简单的链表合并题,难了我好几个小时,最后还是上网搜索了一种不错算法.后期复习完链表的知识我会将我自己的实现代理贴上. 这个算法巧就巧在用了递归的思想, ...
- [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 ...
- 【leetcode❤python】21. Merge Two Sorted Lists
#-*- coding: UTF-8 -*- # Definition for singly-linked list.# class ListNode(object):# def __init ...
- [Leetcode][Python]21: Merge Two Sorted Lists
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 21: Merge Two Sorted Listshttps://oj.le ...
- 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 ...
- 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 ...
- 21.Merge Two Sorted Lists 、23. Merge k Sorted Lists
21.Merge Two Sorted Lists 初始化一个指针作为开头,然后返回这个指针的next class Solution { public: ListNode* mergeTwoLists ...
- [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 ...
随机推荐
- 欧拉函数-bzoj2818-简单推导
This article is made by Jason-Cow.Welcome to reprint.But please post the writer's address. http://ww ...
- AcWing 868. 筛质数 线性筛法
#include <iostream> #include <algorithm> using namespace std; ; int primes[N], cnt; bool ...
- 如何将博客内容输出到pdf
可以按照三类网页插件:Clearly,Instapaper 和 Readability,实际安装发现,第一个装不上,只有最后一个好用.在firefox或者chrom浏览器装好后,右键switch to ...
- 433B.Kuriyama Mirai's Stones
Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from ...
- IDEA导入Git项目后无Git选项
- Nodejs的Gruntjs使用一则
Gruntjs是前端项目构建工具,基于nodejs命令.有些js项目是基于Gruntjs构建的,如Jquery. Gruntjs主要功能有: 1.合并文件. 2.压缩html,js,css,图片文件. ...
- HDU 6631 line symmetric 计算几何
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6631 题意:共\(T\)组数据,每组数据给出\(n\)个点的坐标,这\(n\)个点按顺序给出,相邻的点 ...
- SpringBoot 开发的那些小趣事儿
经过这次在公司实习中获取到的经历,我发现确实有时候书本上的知识发挥的作用微乎其微,好像是被问题打了太极拳一样,你明明想去攻克这个地方,他却给你报了其他地方的错误. 平常的一些小项目根本就不能匹配到企业 ...
- 7、Maven插件
什么是maven插件? maven 实际上是一类依赖插件执行的框架,每个任务实际上是由插件完成,Maven插件通常被用来 创建jar文件 创建war文件 编译代码文件 代码单元测试 创建工程文档 创建 ...
- JAVA(5)之关于main函数的默认放置位置
Eclipse默认主程序入口 Public class 的main函数 package com.study; public class Test { public static void main(S ...