题目:

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的更多相关文章

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

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

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

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

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

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

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

  6. 刷题21. Merge Two Sorted Lists

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

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

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

  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. 关于 redis、memcache、mongoDB 的对比

    从以下几个维度,对 redis.memcache.mongoDB 做了对比. 1.性能 都比较高,性能对我们来说应该都不是瓶颈. 总体来讲,TPS 方面 redis 和 memcache 差不多,要大 ...

  2. 【汉字】转【pīnyīn】

    引言 github地址:aizuyan/pinyin 无意中看到了overtrue/pinyin这个项目,感觉很有意思, 这个项目做了这么一件事情: 将汉字转化为拼音 刚看到这里是不是觉得没什么难度, ...

  3. SNMP Tutorial

    Applications: Internet Management (SNMP) 30.1 Introduction 30.2 The Level Of Management Protocols 30 ...

  4. gdb调试器的使用

    想要使用gdb调试程序的话,首先需要gcc -g main.c -o test 然后运行gdb test对程序进行调试 l (小写的l,是list的首字母),用以列出程序 回车    是运行上一个命令 ...

  5. 安装LNMP(Nginx+Mysql+PHP)

    1:安装nginxyum install -y gcc pcre-devel openssl-develwget http://www.nginx.org/download/nginx-1.4.2.t ...

  6. 移动端自适应:flexible.js可伸缩布局使用

    http://caibaojian.com/flexible-js.html 阿里团队开源的一个库.flexible.js,主要是实现在各种不同的移动端界面实现一稿搞定所有的设备兼容自适应问题. 实现 ...

  7. maven热部署到tomcat

    1添加tomcat用户,在conf/tomcat-user.xml文件下添加一个tomcat用户 <role rolename="manager-gui"/> < ...

  8. html5中画布和SVG的比较

    SVG是基于XML的图形语言,在DOM解析中其每个元素都是可以用的,这样就可以为SCG元素附加JavaScript事件处理器,实现更加丰富的效果. 在SVG中,每个被绘制的图形均被视为对象,如果SVG ...

  9. 看着水了一天的群,终于看到一段高质量的代码了分享一下localStorage

    _history : { //缓存 isLocalStorage:window.localStorage?true:false, set : function(key,value){ //设置缓存 i ...

  10. Android开发加快sdk更新速度

    1.在:\Windows\System32\drivers\etc目录下找到host文件,不能再这个目录修改host文件,需要先拷贝到桌面 然后把一下地址添加到host文件的最末尾 203.208.4 ...