【LeetCode】143. Reorder List 解题报告(Python)
【LeetCode】143. Reorder List 解题报告(Python)
标签(空格分隔): LeetCode
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.me/
题目地址:https://leetcode.com/problems/reorder-list/description/
题目描述:
Given a singly linked list L: L0→L1→…→Ln-1→Ln,
reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…
You may not modify the values in the list’s nodes, only nodes itself may be changed.
Example 1:
Given 1->2->3->4, reorder it to 1->4->2->3.
Example 2:
Given 1->2->3->4->5, reorder it to 1->5->2->4->3.
题目大意
把一个链表的前半部分正序,后半部分逆序,然后一个一个的连接起来。
解题方法
就像题目大意里面说的,需要三步。其实这个题对链表的考察非常的巧妙和详细了,可以说是三个题目了。
代码有点长,就是按照三步来写的。题目要求不能返回新节点,这个也提高了难度。
参考了:[leetcode]Reorder List @ Python
# 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.
"""
if head and head.next and head.next.next:
#find mid
fast, slow = head, head
while fast.next and fast.next.next:
fast = fast.next.next
slow = slow.next
head1 = head
head2 = slow.next
slow.next = None
# reverse linked list head2
dummy = ListNode(0)
dummy.next = head2
p = head2.next
head2.next = None
while p:
temp = p
p = p.next
temp.next = dummy.next
dummy.next = temp
head2 = dummy.next
# merge two linked list head1 and head2
p1 = head1
p2 = head2
while p2:
temp1 = p1.next
temp2 = p2.next
p1.next = p2
p2.next = temp1
p1 = temp1
p2 = temp2
日期
2018 年 6 月 25 日 ———— 新的一周,不要再想烦心事了。
【LeetCode】143. Reorder List 解题报告(Python)的更多相关文章
- 【LeetCode】Reorder List 解题报告
Given a singly linked list L: L0→L1→-→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→- You must do th ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
随机推荐
- 《Redis设计与实现》知识点目录
Redis设计与实现 第一部分 数据结构与对象 第二章 简单动态字符串 p8 简单动态字符串SDS 2.1 SDS的定义 p9 每个sds.h/sdshdr结构表示一个SDS值 2.2 SDS与C字符 ...
- 第一个基础框架 — mybatis框架 — 更新完毕
1.Mybatis是什么? 百度百科一手 提取一下重点: MyBatis 本是apache的一个开源项目iBatis.即:mybatis的原名为:ibatis 2010年迁移到google code, ...
- 日常Java 2021/10/30
Java泛型 Java泛型(generics)是JDK5中引入的一个新特性,泛型提供了编译时类型安全检测机制,该机制允许程序员在编译时检测到非法的类型.泛型的本质是参数化类型,也就是说所操作的数据类型 ...
- 微信小程序的wx.login用async和data解决code不一致的问题
由于wx.login是异步函数,导致在我们获取微信小程序返回的code去请求我们的登录接口时code的值会异常.现在用promise封装一下,将他success的结果返回,在登陆函数中await就可以 ...
- Shell学习(五)—— awk命令详解
一.awk简介 awk是一个非常好用的数据处理工具,相对于sed常常作用于一整个行的处理,awk则比较倾向于一行当中分成数个[字段]处理,因此,awk相当适合处理小型的数据数据处理.awk是一种报 ...
- Swift Storyboard找不到类文件
Swift语言引入了Module概念,在通过关键字@objc(类名)做转换的时候,由于Storyboard没有及时更新Module属性,会导致如下两种类型错误: 1 用@objc(类名)标记的Swif ...
- springmvc框架找那个@responseBody注解
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html& ...
- 声临其境,轻松几步教你把音频变成3D环绕音
在音乐创作.音视频剪辑和游戏等领域中,给用户带来沉浸式音频体验越来越重要.开发者如何在应用内打造3D环绕声效?华为音频编辑服务6.2.0版本此次带来了空间动态渲染功能,可以将人声.乐器等音频元素渲染到 ...
- 什么是网络图(Project)
<Project2016 企业项目管理实践>张会斌 董方好 编著 名词解释:网络图-- 咦?书里没写? 看来张同学也不喜欢名词解释(猜的)哈哈哈-- 但是! 人家写了两种网络图:双代号网络 ...
- CF1445A Array Rearrangement 题解
Content 有 \(t\) 组询问,每组询问给定两个长度为 \(n\) 的数列 \(\{a_i\}_{i=1}^n,\{b_i\}_{i=1}^n\) 和一个整数 \(x\),求是否能够重新对两个 ...