当前位置:网站首页>【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
边栏推荐
- NFT Insider #63:The Sandbox与时代杂志达成合作,YGG成立西班牙subDAO
- opencv 红色区域在哪里?
- Understand (DI) dependency injection in PHP
- Break the memory wall with CPU scheme? Learn from PayPal stack to expand capacity, and the volume of missed fraud transactions can be reduced to 1/30
- Disassembly of Weima prospectus: the electric competition has ended and the intelligent qualifying has just begun
- Musk: Twitter should learn from wechat and make 1billion people "live on it" into a super app
- Create SQLite table with shell script and add SQL statement -- General
- The programmer reality show is coming again! Hulan, as the host, carried the lamp to fill the knowledge. The SSS boss had a bachelor's degree in pharmacy. Zhu Jun and Zhang Min from Tsinghua joined th
- 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
- Amazon's other side in China
猜你喜欢
Development of trading system (V) -- Introduction to Sinovel counter
Hello CTP (III) - CTP quotation API
你真的需要自动化测试吗?
Cesium 拖拽3D模型
[rust submission] review impl trail and dyn trail in rust
数学分析_笔记_第3章:极限
2. play the chromatic harmonica
1. first knowledge of chromatic harmonica
(ultra detailed onenet TCP protocol access) arduino+esp8266-01s accesses the Internet of things platform, uploads real-time collected data /tcp transparent transmission (and how to obtain and write Lu
[harmony OS] [ark UI] basic ETS context operations
随机推荐
Configuration source code
ZABBIX installation pit avoidance Guide
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
Work assessment of pharmaceutical polymer materials of Jilin University in March of the 22nd spring -00025
俄罗斯AIRI研究院等 | SEMA:利用深度迁移学习进行抗原B细胞构象表征预测
Sun Wu plays Warcraft? There is a picture and a truth
Jilin University 22 spring March "official document writing" assignment assessment-00029
ICML 2022 | ByteDance AI Lab proposes a multimodal model: x-vlm, learning multi granularity alignment of vision and language
Work assessment of pharmacotherapeutics of Jilin University in March of the 22nd spring -00064
cesium 图形标注圆形、正方形、多边形、椭圆等
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
Work assessment of Biopharmaceutics of Jilin University in March of the 22nd spring -00005
Crawl Sina Weibo fans
OpenSUSE environment variable settings
On the self-cultivation of an excellent red team member
如何使用IDE自动签名调试鸿蒙应用
Redis related-02
About PLSQL error initialization failure
windows 2003 64位系统php运行报错:1% 不是有效的 win32 应用程序
Work assessment of Biopharmaceutics of Jilin University in March of the 22nd spring -00005