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 ...
随机推荐
- C#连接SQL Server数据库(二)
执行SQL语句:Command对象 1.Command对象概述 Command对象是一个数据命令对象,主要功能是向数据库发送查询.更新.删除.修改操作的SQL语句.Command对象主要有以下几种 ...
- bash_profile文件
bash_profile文件的作用 如何填写 如何生效
- 数据提取之JSON与JsonPATH
数据提取之JSON与JsonPATH JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,它使得人们很容易的进行阅读和编写.同时也方便了机器进行解析和生成.适 ...
- Python实现将图片以二进制格式保存到MySQL数据库中,以及取出:
创建数据库表格式: CREATE TABLE photo ( photo_no int(6) unsigned NOT NULL auto_increment, image MEDIUMBLOB, P ...
- git查漏补缺
1. commit提交注释规范 2. commit 注释没写完或写错了,在不用删除这条commit的情况下,如何更正注释信息 git commit -m '1' git commit --amend ...
- vscode管理员身份运行
管理员身份运行,如vscode 如何设置呢? vscode图标右键 以管理员身份运行程序打钩就行了 运行“在终端打开”的时候,要以管理员身份运行 刚下载完vscode并运行并不是管理员身份会报错 解 ...
- Foreach报错
List<String> a = new ArrayList<String>(); 2 a.add("1"); 3 a.add("2") ...
- 如何在JDBC Connection Configuration配置组件上添加控件
如何在JDBC Connection Configuration配置组件上添加控件 最近项目刚上线,闲来无事又把Jmeter的源码拿出来研究研究,最初的目的是想扒一扒Jmeter里数据库处理的逻辑是怎 ...
- Matlab geom3d函数注释
Matlab geom3d函数解析 geom3d函数库 geom3d库的目的是处理和可视化三维几何原语,如点.线.平面.多面体等.它提供了操作三维几何原语的底层功能,使得开发更复杂的几何算法变得更加容 ...
- Servlet映射细节
Servlet的映射细节: 1):一个Servlet程序(Web组件),可以配置多个<url-pattern>,表示一个Servlet有多个资源名称. <servlet-mappin ...