当前位置:网站首页>【LeetCode】148. Sort linked list
【LeetCode】148. Sort linked list
2022-06-25 04:10:00 【LawsonAbs】
1 subject
2 thought
Divide the linked list into two , Then flip the linked list in the second half , Cross merge the two linked lists after flipping . These operations are relatively simple , But together , It may take a little time to write .
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
边栏推荐
- client-go gin的简单整合十-Update
- Preparedstatement principle of preventing SQL injection
- 如何使用IDE自动签名调试鸿蒙应用
- (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
- Solution to the problem that Linux crontab timed operation Oracle does not execute (crontab environment variable problem)
- 【LeetCode】143. 重排链表
- How to use crawlers to capture bullet screen and comment data of station B?
- SEO的5大关键指标:排名+流量+会话+停留时长+跳出率
- Cesium drag 3D model
- Crawler grabs the idea of reading on wechat
猜你喜欢

opencv怎么安装?opencv下载安装教程

地方/園區產業規劃之 “ 如何進行產業定比特 ”

(超详细onenet TCP协议接入)arduino+esp8266-01s接入物联网平台,上传实时采集数据/TCP透传(以及lua脚本如何获取和编写

Hello CTP (II) -- Introduction to CTP

BGP biplane architecture

Trading system development (IV) - trading counter system

Simple integration of client go gin -update

Maybe it's the wrong reason

1、项目第二阶段——用户注册和登陆

How to install opencv? Opencv download installation tutorial
随机推荐
opencv是开源的吗?
Teach you how to install win11 system in winpe
Development of trading system (VII) -- Analysis of trading delay
Time management understood after working at home | community essay solicitation
Changsha's "talent seeking": "making efforts" and "making practical moves" go hand in hand, "rapid development" and "slow life" go hand in hand
numpy np tips: numpy数组的squeeze等处理
numpy np tips:使用opencv对数组插值放缩到固定形状 cv2.resize(res, dsize=(64, 64), interpolation=cv2.INTER_CUBIC)
Simple integration of client go gin -update
Standing wave ratio calculation method
List rendering in wechat applet
Interview with Mo Tianlun | ivorysql wangzhibin - ivorysql, an Oracle compatible open source database based on PostgreSQL
Jilin University 22 spring March "automatic control principle" work assessment-00050
Development of trading system (XIII) -- Analysis of quickfix source code
[harmony OS] [arkui] ETS development graphics and animation drawing
95% 程序员都在这里摸鱼……
2022-06-21-flink-49 (I. SQL manual)
Color NFT series representing diversity launched on the sandbox market platform
DAP数据调度功能完善说明
The art of writing simple code
BSC parsing input data of transaction