当前位置:网站首页>【LeetCode】23. Merge K ascending linked lists
【LeetCode】23. Merge K ascending linked lists
2022-06-23 03:38:00 【LawsonAbs】
1 subject
There are several points that need attention in this topic :
- Because there are list The linked list in is empty , So you need to delete . But at the same time, because python There are some problems in deleting the list in , So we need to delete in reverse order list The content in .
- You should be proficient in the combination of linked lists . I think this topic should be very familiar , It must be no problem to do it directly , But it's better to write down the questions , It will be faster .
2 thought
This question is not a difficult one , Just a little trouble . It can be regarded as the multiple merging of two linked lists . The only thing is that the current linked list was generated last time .
3 Code
# 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)
# If there are more than two linked lists
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 # The head node of the second linked list
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
# Process remaining nodes
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
边栏推荐
- Analysis on the development status of China's watch industry in 2021: a large number of electric watches are imported [figure]
- Goframe framework: quick creation of static file download web service
- Regeorg actual attack and defense
- What are the advantages and difficulties of introducing AI into ISP Technology
- 【曾书格激光SLAM笔记】Gmapping基于滤波器的SLAM
- How to solve the problem that easynvr cannot be cascaded to the easynvs platform? View ports first
- RTOS system selection for charging point software design
- 1-1VMware介绍
- What is the difference between the poll () method and the remove () method?
- Tencent cloud tcapulusdb helps tmeland, the first virtual Music Carnival in China, and tens of thousands of people cross the new year together!
猜你喜欢

直接插入排序

R tree of search tree

第一批00后下场求职:不要误读他们的“不一样”

centos7 安装 MySQL 及配置 innodb_ruby

Talk about memory model and memory order

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

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

Using jhipster to build microservice architecture

mysql 数据恢复 (.ibdata1, bin log)

Svn local computer storage configuration
随机推荐
An implementation of warning bombing
Brief introduction to arm architecture
直接插入排序
线上MySQL的自增id用尽怎么办?
On the way home from the Spring Festival transportation, traffic visualization will escort you
2022-01-22: Li Kou 411, the abbreviation of the shortest exclusive word. Give a string number
Quickly understand the development status of secondary nodes of industrial Internet identity analysis
The power of code refactoring: how to measure the success of refactoring
[OWT] OWT client native P2P E2E test vs2017 build 3: no test unit comparison, manually generate vs projects
Compile your own openwrt firmware using Tencent cloud lightweight application server
Chapter IV open source projects and deployment
Enterprise official website applet building tutorial
Gx3001q UI instructions for upgrading 8-bit color to 16 bit color
What is the potential of dmail based on Web3.0? First round financing of $10 million?
Which insurance company is the most cost-effective for purchasing serious illness insurance?
Initialize MySQL Gorm through yaml file
Mybatties plus batch warehousing
Insérer le tri directement
Not just offline caching- On how to make good use of serviceworker
How to batch generate jan13 barcode