当前位置:网站首页>21. Merge Two Sorted Lists
21. Merge Two Sorted Lists
2022-07-25 10:18:00 【scwMason】
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
Is to merge two linked lists , Arrange them from small to large
The better way in time and space is :
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
if l1 and l2:
if l1.val>l2.val:
l1,l2=l2,l1
l1.next=self.mergeTwoLists(l1.next,l2)
return l1 or l2keep l1 It is a linked list connecting the smallest node , Through constant exchange l1 and l2 To achieve .
边栏推荐
猜你喜欢
随机推荐
UE4 框架介绍
Record some JS tool functions
DHCP的配置(以华为eNSP为例)
Angr(九)——angr_ctf
数论--负进制转换
Swing的组件图标
Usage of string slicing
多线程——Runnable接口,龟兔赛跑
常用类的小知识
Dynamic planning, shopping list problem
salt常见问题
Swing component
UE4 LoadingScreen动态加载启动动画
shortest-unsorted-continuous-subarray
C3D模型pytorch源码逐句详析(三)
基础背包问题
多线程——Callable接口,lambda
Multithreading deadlock and synchronized
An ASP code that can return to the previous page and automatically refresh the page
虚拟专线网络部署









