当前位置:网站首页>【LeetCode】23. 合并K个升序链表
【LeetCode】23. 合并K个升序链表
2022-06-23 03:32:00 【LawsonAbs】
1 题目
本题有几个需要注意的地方:
- 因为有的list中的链表是空的,所以需要删除。但同时因为python 中的列表删除存在一定的问题,所以我们需要倒序删除list中的内容。
- 链表合并的题要熟练。我觉得这种题目要非常熟悉,直接做肯定是没问题的,但最好还是要记下题,这样会更快些。
2 思想
这题不是难题,只不过是稍有麻烦而已。可以看作是两个链表的多次合并。只不过是当前的一个链表是上次生成的。
3 代码
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:
for i in reversed(range(len(lists))):
if lists[i] is None:
del lists[i]
# print(len(lists))
if len(lists) == 0 or lists[0] is None:
return None
if len(lists) == 1:
return lists[0]
# print(head_1.val)
# 如果存在大于两条链表
while(len(lists)>1):
# merge
head_1 = lists[0] #if len(lists[0]) else None
head_2 = lists[1] #if len(lists[1]) else None # 第二条链表的头结点
if head_1.val <= head_2.val:
head = head_1
head_1 = head_1.next
else:
head = head_2
head_2 = head_2.next
tail = head #
while(head_1 and head_2):
if head_1.val < head_2.val:
tail.next = head_1
head_1 = head_1.next
else:
tail.next = head_2
head_2 = head_2.next
tail = tail.next
# 处理剩余节点
if head_1:
tail.next = head_1
if head_2:
tail.next = head_2
tmp = head
# while(tmp):
# print(tmp.val,end=",")
# tmp = tmp.next
# print("\n")
lists[0] = head
del lists[1]
# print(len(lists))
# break
return head
边栏推荐
- About SSL certificates
- Parsing the implementation of easygbs compatible token as parameter passing
- Analysis on the development status of China's watch industry in 2021: a large number of electric watches are imported [figure]
- Initialize MySQL Gorm through yaml file
- How to batch generate ean14 barcode through TXT file
- 新版kali切换最高账户
- JS counts the number of times a string appears in another string
- R tree of search tree
- Brief introduction to arm architecture
- [Alibaba middleware technology series] "Nacos technology" service registration and discovery related principle analysis
猜你喜欢
![Analysis on the development status of China's watch industry in 2021: a large number of electric watches are imported [figure]](/img/ca/672bfe49c8123da8679b2abeb43a2e.jpg)
Analysis on the development status of China's watch industry in 2021: a large number of electric watches are imported [figure]

新版kali切换最高账户

Encryption related to returnee of national market supervision public service platform
![[quick view] Analysis on the development status and future development trend of the global and Chinese diamond cultivation industry in 2021 [figure]](/img/f1/972a760459a6d599b5681aa634df09.jpg)
[quick view] Analysis on the development status and future development trend of the global and Chinese diamond cultivation industry in 2021 [figure]
![Analysis of China's integrated circuit industry chain in 2021: huge downstream market demand [figure]](/img/de/d73805aaf4345ca3d2a7baf85aab8d.jpg)
Analysis of China's integrated circuit industry chain in 2021: huge downstream market demand [figure]

Jmeter- (V) simulated user concurrent login for interface test

JS Part 4

Detailed discussion on modular architecture design of MCU firmware

Google Earth Engine(GEE)——长时间序列逐月VCI数据提取分析和面积计算(墨西哥为例)

R tree of search tree
随机推荐
[burning] Tencent cloud high tech computing platform HTPC cloud elastic cluster release!
Easygbs service is killed because the user redis is infected with the mining virus process. How to solve and prevent it?
WordPress modifying fixed links and pseudo statics
Flowable refactoring process editor to obtain user information
Implementation process of the new electronic amplification function of easycvr video fusion cloud platform
The difference between code39 and code93
Bi skills - authority control
Tencent released the global DDoS threat report in 2021: DDoS threat has become the preferred extortion method for criminal gangs
SwiftUI 组件大全之使用 ScrollView 和 GeometryReader 创建动画 3D卡片 滚动效果
China's economy has entered the stage of "the third mock examination coexisting", and JD and Shopify have jointly arranged global DTC
Build information query applet by using micro build
Analysis on demand and market scale of China's steamed stuffed bun industry in 2020 [figure]
CFS topics
Learning summary of spanquery source code
Goframe framework (RK boot): enable tls/ssl
JSON. Function of the stringify() optional parameter
JS remove first character of string
How to make distribution box label
Hierarchical attention graph convolution network for interpretable recommendation based on knowledge graph
Troubleshooting and resolution of asydss virtual live broadcast status synchronization and service downtime