当前位置:网站首页>【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
边栏推荐
- Interview with Mo Tianlun | ivorysql wangzhibin - ivorysql, an Oracle compatible open source database based on PostgreSQL
- How far is the memory computing integrated chip from popularization? Listen to what practitioners say | collision school x post friction intelligence
- 《悉达多》:一生之书,可以时常反刍
- 1.初识半音阶口琴
- Jilin University 22 spring March "official document writing" assignment assessment-00084
- DAP数据调度功能完善说明
- 如何使用IDE自动签名调试鸿蒙应用
- Crawler grabs the idea of reading on wechat
- Create SQLite table with shell script and add SQL statement -- General
- 【Rust投稿】捋捋 Rust 中的 impl Trait 和 dyn Trait
猜你喜欢

Internet Explorer died, and netizens started to build a true tombstone

1.初识半音阶口琴

Redis related-02

JSP cannot be resolved to a type error reporting solution

程序猿职业发展9项必备软技能

完美洗牌问题

墨天轮访谈 | IvorySQL王志斌—IvorySQL,一个基于PostgreSQL的兼容Oracle的开源数据库

Russian Airi Research Institute, etc. | SEMA: prediction of antigen B cell conformation characterization using deep transfer learning

《Missing Parts》NFT 作品集第 5 系列上线 The Sandbox 市场平台

Sun Wu plays Warcraft? There is a picture and a truth
随机推荐
Dr. Sun Jian was commemorated at the CVPR conference. The best student thesis was awarded to Tongji Ali. Lifeifei won the huangxutao Memorial Award
【组队学习】SQL编程语言笔记——Task04
Solution to the problem that Linux crontab timed operation Oracle does not execute (crontab environment variable problem)
Standing at the center of the storm: how to change the engine of Tencent
Now, the ear is going into the metauniverse
How to quickly deliver high-value software
居家办公之后才明白的时间管理 | 社区征文
JSP cannot be resolved to a type error reporting solution
Jilin University 22 spring March "official document writing" assignment assessment-00034
Hello CTP (II) -- Introduction to CTP
数学分析_笔记_第3章:极限
The more AI evolves, the more it resembles the human brain! Meta found the "prefrontal cortex" of the machine. AI scholars and neuroscientists were surprised
Configuration source code
2. play the chromatic harmonica
Sun Wu plays Warcraft? There is a picture and a truth
你真的需要自动化测试吗?
AI quantitative transaction (I) -- Introduction to quantitative transaction
Hello CTP (I) - basic knowledge of futures
Trading system development (IV) - trading counter system
Jilin University 22 spring March document retrieval assignment assessment-00073