当前位置:网站首页>【LeetCode】143. 重排链表
【LeetCode】143. 重排链表
2022-06-25 03:54: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
边栏推荐
- Wuenda, the new course of machine learning is coming again! Free auditing, Xiaobai friendly
- 数学分析_笔记_第3章:极限
- windows 2003 64位系统php运行报错:1% 不是有效的 win32 应用程序
- How to use ide to automatically sign and debug Hongmeng application
- 代表多样性的彩色 NFT 系列上线 The Sandbox 市场平台
- Sleep more, you can lose weight. According to the latest research from the University of Chicago, sleeping more than 1 hour a day is equivalent to eating less than one fried chicken leg
- 如何使用IDE自动签名调试鸿蒙应用
- Serious PHP defects can lead to rce attacks on QNAP NAS devices
- web3.eth. Getpendingtransactions() returns null?
- Work assessment of Biopharmaceutics of Jilin University in March of the 22nd spring -00005
猜你喜欢

如何使用IDE自动签名调试鸿蒙应用

Wuenda, the new course of machine learning is coming again! Free auditing, Xiaobai friendly

【Harmony OS】【ArkUI】ets开发 图形与动画绘制
佐喃社区

代錶多樣性的彩色 NFT 系列上線 The Sandbox 市場平臺

JSP cannot be resolved to a type error reporting solution

Zuckerberg's latest VR prototype is coming. It is necessary to confuse virtual reality with reality

opencv 红色区域在哪里?

Mobile mall project operation

Development of trading system (VIII) -- Construction of low delay network
随机推荐
What is the difference between learning code, rolling code and fixed code? The number of repeated codes, coding capacity and the principle of rolling code
Jilin University 22 spring March "technical economics" assignment assessment-00073
zabbix的安装避坑指南
MySQL根据表前缀批量修改、删除表
一文搞懂php中的(DI)依赖注入
Zuckerberg's latest VR prototype is coming. It is necessary to confuse virtual reality with reality
opencv怎么安装?opencv下载安装教程
Windows 2003 64 bit system PHP running error: 1% is not a valid Win32 Application
《悉达多》:一生之书,可以时常反刍
Comprehensive operation of financial risk management X of Dongcai
opencv 红色区域在哪里?
Development of trading system (I) -- Introduction to trading system
OpenSUSE installation pit log
NFT Insider #63:The Sandbox与时代杂志达成合作,YGG成立西班牙subDAO
Amazon's other side in China
Preparedstatement principle of preventing SQL injection
@RequestBody解决获取参数为null
智慧风电:数字孪生 3D 风机智能设备运维
Tensorflow, danger! Google itself is the one who abandoned it
驻波比计算方法