当前位置:网站首页>Force buckle: merging two ordered linked lists
Force buckle: merging two ordered linked lists
2022-06-29 13:17:00 【Fenghua Mingyuan】
The title is merge 2 An ordered list . The main implementation ideas are as follows :
- The linked list is ordered . When comparing, just list1 The value is greater than list2 Of has been inserting data . vice versa .
- You must use a linked list to get through , You can't use arrays
- Pay attention to the empty linked list and the termination conditions
# 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

边栏推荐
- MySQL常用语句和命令汇总
- C # implementation of binary tree non recursive middle order traversal program
- LR、CR纽扣电池对照表
- QT custom control: value range
- Rslo: self supervised lidar odometer (real time + high precision, icra2022)
- Another "provincial capital university", coming!
- 倍福TwinCAT3 的OPC_UA通信测试案例
- C # realize the hierarchical traversal of binary tree
- C # output the middle order traversal through the clue binary tree
- [Junzheng T31] decompression and packaging of read-only rootfs file system squashfs
猜你喜欢

Evaluation of powerful and excellent document management software: image management, book management and document management

从零搭建Pytorch模型教程(四)编写训练过程--参数解析

Qt的信号与槽

Qt中的UI文件介绍

3D model downloading and animation control

Proteus Software beginner notes

Application Service Vulnerability scanning and exploitation of network security skills competition in secondary vocational schools (SSH private key disclosure)

神经网络各个部分的作用 & 彻底理解神经网络

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

Cvpr2022 | knowledge distillation through target aware transformer
随机推荐
Cvpr2022 | knowledge distillation through target aware transformer
C#实现队列结构定义、入队、出队操作
倍福控制第三方伺服走CSV模式--以汇川伺服为例
MySQL常用语句和命令汇总
编写一个shell脚本,求一个数的”逆序数“
STK_GLTF模型
CVPR2022 | A ConvNet for the 2020s & 如何设计神经网络总结
CVPR 2022 | unknown target detection module study: learning unknown targets in video
bind原理及模拟实现
PyGame accurately detects image collision
Interview shock 61: tell me about MySQL transaction isolation level?
Precautions for Beifu controller connecting Panasonic EtherCAT servo
Qt中的UI文件介绍
Acwing game 57
STK_ Gltf model
C#实现二叉树的层次遍历
mysql调优
leetcode 522. Longest special sequence II
Comparison table of LR and Cr button batteries
NvtBack