21. Merge Two Sorted Lists —— Python
题目:
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.
没事来做做题,该题目是说两个排序好的链表组合起来,依然是排序好的,即链表的值从小到大。
代码:
于是乎,新建一个链表,next用两个链表当前位置去比较,谁的小就放谁。当一个链表放完之后,说明另外一个链表剩下的元素都比较大,再放进去就好。
该题目简单,因为已经是两个排序好的链表了。
以下是Python代码,并有测试过程。
#coding:utf-8
# Definition for singly-linked list.
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None class Solution(object):
def mergeTwoLists(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
if not l1 and not l2: return
result = ListNode(0)
l = result
while l1 and l2:
if l1.val < l2.val:
l.next = l1
l1 = l1.next
else:
l.next = l2
l2 = l2.next
#融合后链表的下一位,当前位置刚刚赋值
l = l.next
#把剩余的链表排在后面
l.next = l1 or l2
#返回融合后链表从第二个对象开始,第一个对象是自己创建的ListNode(0)
return result.next if __name__=='__main__':
#创建l1和l2两个链表,注意,排序好的就需要arr1和arr2中数字从小到大
arr1 = [1,2,3]
arr2 = [5,6,7]
l1 = ListNode(arr1[0])
p1 = l1
l2 = ListNode(arr2[0])
p2 = l2
for i in arr1[1:]:
p1.next = ListNode(i)
p1 = p1.next
for i in arr2[1:]:
p2.next = ListNode(i)
p2 = p2.next
s=Solution()
#融合两个链表
q=s.mergeTwoLists(l1,l2)
21. Merge Two Sorted Lists —— Python的更多相关文章
- [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(照搬大神做法)
1.题目 21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new l ...
- 刷题21. Merge Two Sorted Lists
一.题目说明 这个题目是21. Merge Two Sorted Lists,归并2个已排序的列表.难度是Easy! 二.我的解答 既然是简单的题目,应该一次搞定.确实1次就搞定了,但是性能太差: R ...
- C# 写 LeetCode easy #21 Merge Two Sorted Lists
21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list s ...
- [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 ...
- [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 ...
随机推荐
- AutoMapper简单用法
首先在NuGet添加AutoMapper /// <summary> /// AutoMapper帮助类 /// </summary> public static class ...
- ElasticSearch中bulkProcesser使用
初次接触es,可能对增删改查很熟悉,以为能为得心应手,本次应用场景为 数据库变更一条记录,会触发更新es中的数据,每秒并发大概30条左右,测试环境一切工作正常(数据量较少),上线后发现日志中很多类似于 ...
- JSON资料整理
http://www.cnblogs.com/zxlovenet/p/3566802.html
- 借助Html制作渐变的网页背景颜色
借助Html制作渐变的网页背景颜色 <html> <head> <title>制作渐变背景</title> <meta http-equiv=&q ...
- Sed、Awk单行脚本快速参考
文本间隔: # 在每一行后面增加一空行 sed G awk '{printf("%s\n\n",$0)}' # 将原来的所有空行删除并在每一行后面增加一空行. # 这样在输出的文本 ...
- gradle多模块开发
参考文档:gradle的官方userguide.pdf文档的chapter 55和chapter 56.gradle的多模块或项目开发一定不会比maven差,在我看来!大的项目分成多个模块来开发是常事 ...
- Java 泛型 <? super T> 中 super 怎么 理解?与 extends 有何不同?
看知乎:https://www.zhihu.com/question/20400700 了解的越多,就会发现,越多不了解.
- Unity3D性能优化
一.美术资源优化 1.动态物体,角色.怪物.NPC (1)控制面的数量,300-2000个 (2)控制Skinner Mesh Renderer的数量,1个 (3)控制材质数量,1-3个 (4)控 ...
- 信息安全比赛总结(21ic转帖)
我们的题目是基于这个ZedBoard的__视频流的人脸识别识别的算法采用的是PCA,,但是在后期的调试和实验中发现,,PCA的效果很容易受到环境,比如光照强度,背景,摄像头像素等影响:如果后期的改进的 ...
- C++ 操作XML文件 使用MSXML.DLL
使用MSXML.DLL读写XML; 文件顶部加入 #import "msxml3.dll"; using namespace MSXML2; //这两句作用是,在程序的文件夹下生成 ...