当前位置:网站首页>力扣:合并两个有序链表
力扣:合并两个有序链表
2022-06-29 12:28:00 【风华明远】
该题目是合并2个有序的链表。主要实现思路如下:
- 链表是有序的。比较的时候只要list1的值大于list2的中的就一直插入数据。反之亦然。
- 要用链表才能通过,不能使用数组
- 注意空链表以及终止条件
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def appendList(self,list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
while list2:
list1.next = ListNode(list2.val)
list1 = list1.next
list2 = list2.next
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
if not list2:
return list1
if not list1:
return list2
if list1.val > list2.val:
r = m = ListNode(list2.val)
list2 = list2.next
if not list2:
self.appendList(m,list1)
return r
else:
r = m = ListNode(list1.val)
list1 = list1.next
while list1:
while list1.val > list2.val:
m.next = ListNode(list2.val)
m = m.next
list2 = list2.next
if not list2:
self.appendList(m,list1)
return r
m.next = ListNode(list1.val)
m = m.next
list1 = list1.next
self.appendList(m,list2)
return r

边栏推荐
- C binary tree structure definition and node value addition
- 从零搭建Pytorch模型教程(五)编写训练过程--一些基本的配置
- Cvpr2022 | panopticdepth: a unified framework for depth aware panoramic segmentation
- C # implements definition, insertion and construction of binary sort tree
- C语言的指针详解
- OPC of Beifu twincat3_ UA communication test case
- Matlab简单入门
- Qt中的UI文件介绍
- Viewing splitchunks code segmentation from MPX resource construction optimization
- The former security director of Uber faced fraud allegations and concealed the data leakage event
猜你喜欢

Tutorial on building pytoch model from zero (V) writing training process -- some basic configurations

趣谈网络协议(二)传输层

QT custom control: value range

倍福TwinCAT3 的OPC_UA通信测试案例

Precautions for Beifu controller connecting Panasonic EtherCAT servo

中职网络安全技能竞赛之应用服务漏洞扫描与利用(SSH私钥泄露)

Cnpm reports an error 'cnpm' is not an internal or external command, nor is it a runnable program or batch file

Qt中的UI文件介绍

C#线索二叉树的定义

Simple introduction to matlab
随机推荐
Comparison table of LR and Cr button batteries
Testing -- automated testing: about the unittest framework
Don't build the wheel again. It is recommended to use Google guava open source tool class library. It is really powerful!
The former security director of Uber faced fraud allegations and concealed the data leakage event
C # output the middle order traversal through the clue binary tree
C#通过线索二叉树进行中序遍历输出
Uber前安全主管面临欺诈指控 曾隐瞒数据泄露事件
MySQL常用语句和命令汇总
三维模型下载与动画控制
Hystrix断路器
Install the terrain ovirt plug-in to provide automated management for ovirt
Principle and Simulation of bind
C#通过中序遍历对二叉树进行线索化
OPC of Beifu twincat3_ UA communication test case
CVPR 2022 | unknown target detection module study: learning unknown targets in video
Cvpr2022 | a convnet for the 2020s & how to design neural network Summary
趣谈网络协议(二)传输层
倍福TwinCAT配置、调试第三方伺服详细讲解--以汇川IS620N为例子
Interview shock 61: tell me about MySQL transaction isolation level?
C # implements the operations of sequence table definition, insertion, deletion and search