当前位置:网站首页>【LeetCode】148. 排序链表
【LeetCode】148. 排序链表
2022-06-25 03:53:00 【LawsonAbs】
1 题目
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 reorderList(self, head: ListNode) -> None:
""" Do not return anything, modify head in-place instead. """
mid = self.splitList(head)
# 将 mid 开头的这部分链表翻转
head2 = self.reverseList(mid)
return self.mergeList(head,head2)
# 二分链表
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
# 翻转链表
def reverseList(self,head):
pre = None
start = head
while(start):
tmp = start.next
start.next = pre
pre = start
start = tmp
return pre
# 交叉合并
def mergeList(self,head1,head2):
cnt = 0
tail = head = head1 # 最后返回的头节点
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
边栏推荐
- Windows 2003 64 bit system PHP running error: 1% is not a valid Win32 Application
- Development of trading system (VII) -- Analysis of trading delay
- 《悉达多》:一生之书,可以时常反刍
- Qt编译数据库插件通用步骤说明
- Development of trading system (V) -- Introduction to Sinovel counter
- Work assessment of pharmaceutical polymer materials of Jilin University in March of the 22nd spring -00025
- 2. play the chromatic harmonica
- js工具函数,自己封装一个节流函数
- Jilin University 22 spring March "official document writing" assignment assessment-00034
- Development of trading system (XI) -- Introduction to quickfix
猜你喜欢

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

Does it count as staying up late to sleep at 2:00 and get up at 10:00? Unless you can do it every day

opencv 红色区域在哪里?

智慧风电:数字孪生 3D 风机智能设备运维

ICML 2022 | ByteDance AI Lab proposes a multimodal model: x-vlm, learning multi granularity alignment of vision and language

Peking University has a new president! Gongqihuang, academician of the Chinese Academy of Sciences, took over and was admitted to the Physics Department of Peking University at the age of 15

Disassembly of Weima prospectus: the electric competition has ended and the intelligent qualifying has just begun

Hello CTP (IV) - CTP transaction API

client-go gin的简单整合十一-Delete

AI writes its own code to let agents evolve! The big model of openai has the flavor of "human thought"
随机推荐
Development of trading system (VIII) -- Construction of low delay network
Cesium 拖拽3D模型
Interview with Mo Tianlun | ivorysql wangzhibin - ivorysql, an Oracle compatible open source database based on PostgreSQL
X86 CPU, critical! The latest vulnerability has caused heated discussion. Hackers can remotely steal keys. Intel "all processors" are affected
2D 照片变身 3D 模型,来看英伟达的 AI 新“魔法”!
The 5th series of NFT works of missing parts was launched on the sandbox market platform
NFT Insider #63:The Sandbox与时代杂志达成合作,YGG成立西班牙subDAO
练习:仿真模拟福彩双色球——中500w巨奖到底有多难?跑跑代码就晓得了。
OpenSUSE installation pit log
Jilin University 22 spring March "automatic control principle" work assessment-00050
2.吹响半音阶口琴
Development of trading system (XII) - Official quickfix document
Teach you how to install win11 system in winpe
Maintenant, les oreilles vont entrer dans le métacosme.
Lao Ye's blessing
程序猿职业发展9项必备软技能
9 necessary soft skills for program ape career development
Standing at the center of the storm: how to change the engine of Tencent
Windows 2003 64 bit system PHP running error: 1% is not a valid Win32 Application
Jilin University 22 spring March document retrieval assignment assessment-00073