题目描述:

方法一:

# 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:
res = ListNode(None)
node = res
while l1 and l2:
if l1.val<l2.val:
node.next,l1=l1,l1.next
else:
node.next,l2=l2,l2.next
node = node.next
if l1:
node.next=l1
else:
node.next=l2
return res.next

方法二:递归

class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
if l1 is None and l2 is None:
return None
if l1 is None:
return l2
if l2 is None:
return l1 if l1.val > l2.val:
l2.next = self.mergeTwoLists(l1, l2.next)
return l2 l1.next = self.mergeTwoLists(l1.next, l2)
return l1

python 数据结构之单链表的实现

链接:https://www.cnblogs.com/yupeng/p/3413763.html

最新文章

  1. [LeetCode] Design Hit Counter 设计点击计数器
  2. [js开源组件开发]localStorage-cache本地存储的缓存管理
  3. mysql 将时间戳直接转换成日期时间
  4. POJ 1113 Wall(Graham求凸包周长)
  5. HTML5 History对象,Javascript修改地址栏而不刷新页面
  6. Docker 组件如何协作?- 每天5分钟玩转容器技术(8)
  7. Java:参数数量可变的方法
  8. MysqL自动提交机制的关闭
  9. eclipse 导入gradle引入多模块项目,引入eclipse后变成了好几个工程
  10. Simditor 富文本编辑器多选图片上传、视频连接插入
  11. “多个单核CPU”与“单个多核CPU”哪种方式性能较强?
  12. 爬虫3 requests基础2 代理 证书 重定向 响应时间
  13. index-document-shard
  14. Python使用matplotlib模块绘制多条折线图、散点图
  15. LoadRunner中的IP欺骗的设置以及误区
  16. [PA2014]Pakowanie
  17. Reverting back to the R12.1.1 and R12.1.3 Homepage Layout
  18. oracle过程书写规范
  19. Android中Activity的四种启动方式
  20. http协议详解(2)

热门文章

  1. CF963E Circles of Waiting
  2. SQL Wildcards 通配符
  3. python--内置函数、匿名函数、递归调用
  4. SCP-bzoj-1085
  5. Cisco基础(五):配置静态NAT、配置端口映射、配置动态NAT、PAT配置、办公区Internet的访问
  6. appium-Android的驱动程序
  7. 调用windows的复制文件对话框
  8. Linux CentOS-7.4-x86_64(原版) 百度网盘下载
  9. echarts(4.0版本)
  10. 使用Intent实现Activity之间传值与跳转(转)