原题地址:http://oj.leetcode.com/problems/reorder-list/

题意:

Given a singly linked list LL0→L1→…→Ln-1→Ln,
reorder it to: L0→LnL1→Ln-1→L2→Ln-2→…

You must do this in-place without altering the nodes' values.

For example,
Given {1,2,3,4}, reorder it to {1,4,2,3}.

解题思路:1,先将链表截断为两个相等长度的链表,如果链表长度为奇数,则第一条链表长度多1。如原链表为L={1,2,3,4,5},那么拆分结果为L1={1,2,3};L2={4,5}。拆分的技巧还是快慢指针的技巧。

       2,将第二条链表L2翻转,如将L2={4,5}翻转为L2={5,4}。

3,按照题意归并链表。

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
# @param head, a ListNode
# @return nothing
def reorderList(self, head):
if head==None or head.next==None or head.next.next==None: return head # break linked list into two equal length
slow = fast = head #快慢指针技巧
while fast and fast.next: #需要熟练掌握
slow = slow.next #链表操作中常用
fast = fast.next.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 #将p指向的节点一个一个插入到dummy后面
while p: #就完成了链表的翻转
tmp=p; p=p.next #运行时注意去掉中文注释
tmp.next=dummy.next
dummy.next=tmp
head2=dummy.next # merge two linked list head1 and head2
p1 = head1; p2 = head2
while p2:
tmp1 = p1.next; tmp2 = p2.next
p1.next = p2; p2.next = tmp1
p1 = tmp1; p2 = tmp2

[leetcode]Reorder List @ Python的更多相关文章

  1. 【leetcode】Reorder List (python)

    问题的思路是这样: 循环取头部合并,事实上也能够换个角度来看,就是将后面的链表结点,一次隔空插入到第一部分的链表中. class Solution: # @param head, a ListNode ...

  2. [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 ...

  3. [LeetCode]题解(python):125 Valid Palindrome

    题目来源 https://leetcode.com/problems/valid-palindrome/ Given a string, determine if it is a palindrome ...

  4. [LeetCode]题解(python):120 Triangle

    题目来源 https://leetcode.com/problems/triangle/ Given a triangle, find the minimum path sum from top to ...

  5. [LeetCode]题解(python):119 Pascal's Triangle II

    题目来源 https://leetcode.com/problems/pascals-triangle-ii/ Given an index k, return the kth row of the ...

  6. [LeetCode]题解(python):118 Pascal's Triangle

    题目来源 https://leetcode.com/problems/pascals-triangle/ Given numRows, generate the first numRows of Pa ...

  7. [LeetCode]题解(python):116 Populating Next Right Pointers in Each Node

    题目来源 https://leetcode.com/problems/populating-next-right-pointers-in-each-node/ Given a binary tree ...

  8. [LeetCode]题解(python):114 Flatten Binary Tree to Linked List

    题目来源 https://leetcode.com/problems/flatten-binary-tree-to-linked-list/ Given a binary tree, flatten ...

  9. [LeetCode]题解(python):113 Path Sum II

    题目来源 https://leetcode.com/problems/path-sum-ii/ Given a binary tree and a sum, find all root-to-leaf ...

随机推荐

  1. iOS 11开发教程(十)iOS11无线连接手机真机测试

    iOS 11开发教程(十)iOS11无线连接手机真机测试 在Xcode 9.0中,已经可以通过无线连接手机进行真机测试了.具体的操作步骤如下: (1)首先需要使用数据线将手机连接到苹果电脑上. (2) ...

  2. 联系表单 1_copy

    你的名字 (必填) [text* your-name] 你的邮箱 (必填) [email* your-email] 主题 [text your-subject] 你的留言 [textarea your ...

  3. AGC009D Uninity

    一些无关紧要的事: 似乎很久没写题解了……象征性地更一篇.另外很多blog都设了私密,不是很敢公开,不过说不定哪天会公开的. link 题意: 最优树的点分治:使得点分最大层数最小.(听说是经典问题) ...

  4. [SPOJ-BEADS]Glass Beads

    来源: CE1998 题目大意: 求字符串最小表示. 思路: 字符串复制一遍接在后面,构建SAM,然后每次跑小的转移. 跑n次以后就跑到了最小表示的末尾,用该状态的len值减去n就是最小表示的起始位置 ...

  5. Git 历险记

    Git历险记(一) 作为分布式版本控制系统的重要代表--Git已经为越来越多的人所认识,它相对于我们熟悉的CVS.SVN甚至同时分布式控制系统的Mercurial,有哪些优势和不足呢.这次InfoQ中 ...

  6. bzoj 3931 最短路+最大流

    较水,但因为范围问题WA了两次.... /************************************************************** Problem: 3931 Us ...

  7. 安卓中WebKit的使用

    1.在安卓开发中,使用webkit显示网页 步骤: ①初始化一个webkit控件: ②获取webkit的WebSettings对象: ③设置javascript为enable ④为webkit设置&q ...

  8. CodeForces 128D Numbers 构造

    D. Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

  9. vue过滤器在v2.0版本用法

    vue 1.x 的写法在  vue 2.x版本已经废除 vue 1.x 写法 <body> <div id="app"> {{message | capit ...

  10. 解决Xilinx ISE在Win8下打开崩溃闪退的方法

    http://www.121down.com/article/article_13651.html 坑爹的ISE对win8无法完美支持(包括目前最新的14.6),在使用64位ISE时点击OPEN之类的 ...