当前位置:网站首页>【LeetCode】143. Rearrange linked list
【LeetCode】143. Rearrange linked list
2022-06-25 04:10:00 【LawsonAbs】
1 subject
2 thought
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
边栏推荐
- SQL, CTE, FLG CASE问题
- 长沙“求才”:“下力气”与“出实招”并进,“快发展”和“慢生活”兼得
- The art of writing simple code
- General steps for QT compiling database plug-ins
- Work assessment of pharmaceutical polymer materials of Jilin University in March of the 22nd spring -00025
- Development of trading system (VI) -- HFT high frequency trading
- Jilin University 22 spring March "automatic control principle" work assessment-00050
- Development of trading system (VIII) -- Construction of low delay network
- OpenSUSE installation pit log
- Simple integration of client go gin -update
猜你喜欢
Serious PHP defects can lead to rce attacks on QNAP NAS devices
ZABBIX installation pit avoidance Guide
Mobile mall project operation
Do you really need automated testing?
Crawler grabs the data of Douban group
【openwrt】推荐一个国内开发的openwrt的版本,iStoreOS简介,非常好用,主要是做了一些优化。解决了汉化的问题。
2. play the chromatic harmonica
Is opencv open source?
WMS仓储管理系统的使用价值,你知道多少
Maybe it's the wrong reason
随机推荐
1280_C语言求两个无符号整形的平均值
The 5th series of NFT works of missing parts was launched on the sandbox market platform
微信小程序中的列表渲染
数字时代的“文艺复兴”?起底数字藏品,让人欢喜让人愁
Work assessment of pharmacotherapeutics of Jilin University in March of the 22nd spring -00064
MySQL插入过程报错1062,但是我没有该字段。
OpenSUSE environment PHP connection Oracle
Color NFT series representing diversity launched on the sandbox market platform
acmStreamOpen返回值问题
[harmony OS] [arkui] ETS development graphics and animation drawing
client-go gin的简单整合十一-Delete
How many images can opencv open?
95% 程序员都在这里摸鱼……
9 necessary soft skills for program ape career development
升级cmake
Cesium 加载显示热力图
《Missing Parts》NFT 作品集第 5 系列上线 The Sandbox 市场平台
[proteus simulation] Arduino uno key controls the flashing increase / decrease display of nixie tube
How to install opencv? Opencv download installation tutorial
【Kubernetes系列】Helm的安装使用