首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Leetcode-Easy21. Merge Two Sorted ListsDefinition for singly-linked list.class ListNode:def init(sel

Leetcode-Easy21. Merge Two Sorted ListsDefinition for singly-linked list.class ListNode:def init(sel

作者头像
致Great
发布2018-04-11 16:37:20
5710
发布2018-04-11 16:37:20
举报
文章被收录于专栏:程序生活程序生活

21. Merge Two Sorted Lists

  • 描述: 将两个有序链表进行合并,合并之后的链表也是有序链表
  • 思路: 递归
  • 代码

Definition for singly-linked list.

class ListNode:

def init(self, x):

self.val = x

self.next = None

class Solution:
 def mergeTwoLists(self, l1, l2):
 # if not l1 or not l2:
 #     return l1 or l2
 if l1==None:
 return l2
 if l2==None:
 return l1
 if l1.val < l2.val:
 l1.next = self.mergeTwoLists(l1.next, l2)
 return l1
 else:
 l2.next = self.mergeTwoLists(l1, l2.next)
 return l2
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.03.17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 21. Merge Two Sorted Lists
  • Definition for singly-linked list.
  • class ListNode:
  • def init(self, x):
  • self.val = x
  • self.next = None
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档