当前位置:网站首页>【LeetCode】148. Sort linked list
【LeetCode】148. Sort linked list
2022-06-25 04:10:00 【LawsonAbs】
1 subject
2 thought
Divide the linked list into two , Then flip the linked list in the second half , Cross merge the two linked lists after flipping . These operations are relatively simple , But together , It may take a little time to write .
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 reorderList(self, head: ListNode) -> None:
""" Do not return anything, modify head in-place instead. """
mid = self.splitList(head)
# take mid The first part of the list is flipped
head2 = self.reverseList(mid)
return self.mergeList(head,head2)
# Binary linked list
def splitList(self,head):
slow = fast = head
while(fast.next and fast.next.next):
fast = fast.next.next
slow = slow.next
tmp = slow.next
slow.next = None
return tmp
# Flip list
def reverseList(self,head):
pre = None
start = head
while(start):
tmp = start.next
start.next = pre
pre = start
start = tmp
return pre
# Cross merger
def mergeList(self,head1,head2):
cnt = 0
tail = head = head1 # Last returned header node
head1 = head1.next
while(head1 and head2):
if cnt % 2 == 0:
tail.next = head2
head2 = head2.next
else:
tail.next = head1
head1 = head1.next
tail = tail.next
cnt += 1
if head1:
tail.next = head1
if head2:
tail.next = head2
return head
边栏推荐
- BSC parsing input data of transaction
- La gamme NFT Color, qui représente la diversité, est en ligne sur la plate - forme du marché Sandbox
- Jilin University 22 spring March new development English comprehensive course (I) assignment assessment-00080
- Cesium loading display thermal diagram
- Development of trading system (x) -- fix agreement
- acmStreamOpen返回值问题
- [team learning] SQL programming language notes - task04
- Cesium 拖拽3D模型
- 2D 照片变身 3D 模型,来看英伟达的 AI 新“魔法”!
- opencv最大能打开多少图像?
猜你喜欢

“语法糖”——我的编程新知

Where is the red area of OpenCV?

Cesium 拖拽3D模型
![[harmony OS] [arkui] ETS development graphics and animation drawing](/img/9d/0ac2b3d8bcdcd610767df930e2fa4e.png)
[harmony OS] [arkui] ETS development graphics and animation drawing

Interview with Mo Tianlun | ivorysql wangzhibin - ivorysql, an Oracle compatible open source database based on PostgreSQL

Exercise: how difficult is it to simulate the blessing lottery two-color ball - China 500W grand prize? Just run the code.

opencv怎么安装?opencv下载安装教程

Development of trading system (III) - risk control system

acmStreamOpen返回值问题

文本关键词提取:ansj
随机推荐
文本关键词提取:ansj
代錶多樣性的彩色 NFT 系列上線 The Sandbox 市場平臺
Solution to the problem that Linux crontab timed operation Oracle does not execute (crontab environment variable problem)
[harmony OS] [ark UI] basic ETS context operations
1280_C语言求两个无符号整形的平均值
[harmony OS] [arkui] ETS development graphics and animation drawing
《Missing Parts》NFT 作品集第 5 系列上线 The Sandbox 市场平台
@RequestBody解决获取参数为null
The problem that only the home page can be accessed under openSUSE Apache laravel
AI quantitative transaction (II) -- tushare financial data framework
Qt编译数据库插件通用步骤说明
【Harmony OS】【ARK UI】ETS 上下文基本操作
Mysql的order by
PHP代码审计1—PHP.ini的那些事
Simple integration of client go gin -update
Development of trading system (V) -- Introduction to Sinovel counter
La gamme NFT Color, qui représente la diversité, est en ligne sur la plate - forme du marché Sandbox
Is opencv open source?
Simple integration of client go gin 11 delete
How many images can opencv open?