# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
# def reorderList(self, head):
# """
# :type head: ListNode
# :rtype: void Do not return anything, modify head in-place instead.
# """
# # List as index to rebuild relation
# if not head:
# return
# dmap = []
# current = head
# while current is not None:
# dmap.append(current)
# current = current.next
# ls = len(dmap)
# for i in range(ls / 2):
# t = -1 * (i + 1)
# dmap[t].next = dmap[i].next
# dmap[i].next = dmap[t]
# dmap[ls / 2].next = None
def reorderList(self, head):
# Two points
if head is None or head.next is None:
return
p1, p2 = head, head.next
while p2 and p2.next:
p1 = p1.next
p2 = p2.next.next
head2 = p1.next
p1.next = None
p2 = head2.next
head2.next = None
# reverse mid->end to end->mid
while p2:
temp = p2.next
p2.next = head2
head2 = p2
p2 = temp
p1, p2 = head, head2
# merge
while p1:
temp = p1.next
p1.next = p2
p1 = p1.next
p2 = temp
python-leetcode面试题解之第143题重排链表-题解.zip
需积分: 1 43 浏览量
2024-05-14
05:47:59
上传
评论
收藏 1KB ZIP 举报
__AtYou__
- 粉丝: 1640
- 资源: 488
最新资源
- Windows使用docker搭建多个GPU cuda版本的深度学习环境并用VSCode的SSH连接环境开发
- python从一个CSV文件中读取数据,对数据进行一些基本的数据清洗和分析,并使用matplotlib生成数据的可视化
- 文本分类-基于Pytorch实现的文本分类算法-附项目源码-优质项目实战.zip
- 五子棋游戏代码Python
- python读取一个包含销售数据的CSV文件,计算每个月的总销售额,并绘制出销售额的月变化图
- 一个简单的前端开发脚本示例,该脚本使用JavaScript和HTML来创建一个简单的网页,该网页包含一个按钮
- 基于JavaScript的餐饮外卖系统设计源码
- 仪器程控动态库NationalInstruments.NI4882.dll
- python读取CSV文件,进行数据处理绘制数据图
- 华为NAS管理工具OceanStor S2200T
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈