【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)的更多相关文章

  1. 【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 ...

  2. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  3. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  4. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  5. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  6. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  7. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  8. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

  9. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

随机推荐

  1. LearnPython_week4

    1.装饰器2.生成器3.迭代器4.内置方法5.可序列化6.项目规范化 1.装饰器 # -*- coding:utf-8 -*- # Author:Wong Du ### 原代码 def home(): ...

  2. 初学者如何吃透一个Java项目

    不少初学者朋友在学习Java过程中,会对着视频敲Java项目,其中遇到的BUG还能解决,但就是每次敲完一个项目,就感觉很空虚,项目里面的知识点感觉懂了但又好像没懂 这些朋友应该怎样才能掌握一个项目所用 ...

  3. Sharding-JDBC 简介

    什么是Sharding-JDBC 1.是轻量级的 java 框架,是增强版的 JDBC 驱动2. Sharding-JDBC(1)主要目的是:简化对分库分表之后数据相关操作.不是帮我们做分库分表,而是 ...

  4. Linux基础命令---ntpq查询时间服务器

    ntpq ntpq指令使用NTP模式6数据包与NTP服务器通信,能够在允许的网络上查询的兼容的服务器.它以交互模式运行,或者通过命令行参数运行. 此命令的适用范围:RedHat.RHEL.Ubuntu ...

  5. Type difference of character literals in C and C++

    Every literal (constant) in C/C++ will have a type information associated with it. In both C and C++ ...

  6. shell脚本计算Linux网卡流量

    本文介绍了计算linux网卡流量的一个shell脚本,一个通过固定间隔时间获取ifconfig eth0 的字节值而计算出网卡流量的方法,有需要的朋友参考下. 使用shell脚本计算Linux网卡流量 ...

  7. JDBCUtils工具类的属性

    package cn.itcast.util;import java.io.FileReader;import java.io.IOException;import java.net.URL;impo ...

  8. 【JavaWeb】【JSP】【Bean】JavaBean基础使用方法与操作步骤

    JavaBean基础使用方法与操作步骤 JavaWeb jsp Bean 项目结构 JavaBean的概念 JavaBean是可复用的.平台独立的软件组件 JavaBean既可以是简单的GUI要素,如 ...

  9. JDK各版本新增的主要特性总结

    JDK1.5新特性: 1.自动装箱与拆箱: 2.枚举 3.静态导入,如:import staticjava.lang.System.out 4.可变参数(Varargs) 5.内省(Introspec ...

  10. 甘特图中与任务有关的操作(Project)

    <Project2016 企业项目管理实践>张会斌 董方好 编著 任务建好了不是马上就没事了,后面还有各种事,比如像我这种粗心的,冷不丁就会冒出个"通假字"什么的,难道 ...