[leetcode]Reorder List @ Python
原题地址:http://oj.leetcode.com/problems/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 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的更多相关文章
- 【leetcode】Reorder List (python)
问题的思路是这样: 循环取头部合并,事实上也能够换个角度来看,就是将后面的链表结点,一次隔空插入到第一部分的链表中. class Solution: # @param head, a ListNode ...
- [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]题解(python):125 Valid Palindrome
题目来源 https://leetcode.com/problems/valid-palindrome/ Given a string, determine if it is a palindrome ...
- [LeetCode]题解(python):120 Triangle
题目来源 https://leetcode.com/problems/triangle/ Given a triangle, find the minimum path sum from top to ...
- [LeetCode]题解(python):119 Pascal's Triangle II
题目来源 https://leetcode.com/problems/pascals-triangle-ii/ Given an index k, return the kth row of the ...
- [LeetCode]题解(python):118 Pascal's Triangle
题目来源 https://leetcode.com/problems/pascals-triangle/ Given numRows, generate the first numRows of Pa ...
- [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 ...
- [LeetCode]题解(python):114 Flatten Binary Tree to Linked List
题目来源 https://leetcode.com/problems/flatten-binary-tree-to-linked-list/ Given a binary tree, flatten ...
- [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 ...
随机推荐
- 使用 Python 可以做什么?
翻译自 <Python学习手册(第5版)> Systems Programming Python 对操作系统服务的内置接口使其非常适合编写可移植.可维护的系统管理工具和实用程序 utili ...
- C#语言-NPOI.dll导入Excel功能的实现
前言:刚工作那会,公司有一套完善的MVC框架体系,每当有导入EXCEL功能要实现的时候,都会借用框架里自带的导入方法,一般三下五除二就完成了,快是快,可总是稀里糊涂的,心里很没有底.前几天,在另一个原 ...
- hdu-1540线段树刷题
title: hdu-1540线段树刷题 date: 2018-10-18 19:55:21 tags: acm 刷题 categories: ACM-线段树 概述 哇,,,这道线段树的题可以说是到目 ...
- Python发送带附件的邮件
看别人的博客就不要在往别人的邮箱里发东西了行不行, 有点素质可以吗!!! 写出来分享还不知道珍惜!!!!! #-*-encoding:utf-8 -*- import os import smtpli ...
- 【20181103T2】图【结论+bfs最短路】
一眼最短路 --感觉是个结论啊 建超级源汇? 什么鬼 合并ab和cd? 不一样的吗 开始想的至少有一条路径是最短路 然后发现不对: 开始对着这个图瞎想 从B开始找A的最短路,然后把到B小于等于的边赋成 ...
- BZOJ 1528 [POI2005]sam-Toy Cars(优先队列)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1528 [题目大意] 地上最多可以放k个玩具,现在给出需求顺序, 问最少需要去架子上拿几 ...
- 体感设备:因特尔 Intel RealSense R200,乐视LeTV Pro Xtion和Orb奥比中光bec Astra比较
最近调试三个个厂家的体感设备,第一个是Intel的RealSense R200(参数规格:分辨率:1080p,深度有效距离:0.51-4,USB3.0),第二个是乐视LeTV Pro Xtion(参数 ...
- paypal对接
paypal支付接口准备工作 首先去申请一个paypal账号,https://www.paypal.com/. 申请完毕并登录,进入https://developer.paypal.com/devel ...
- MySQL的冷备份和热备份概念理解(转)
一.冷备份(off, 慢, 时间点上恢复) 冷备份发生在数据库已经正常关闭的情况下,当正常关闭时会提供给我们一个完整的数据库.冷备份是将关键性文件拷贝到另外位置的一种说法.对于备份数据库信息而言,冷备 ...
- ATMEL精妙的IRQ中断处理过程
A: 从栈地址开始,栈顶为AT91SAM7S64的16K片内RAM尽头0x00204000IRQ_STACK_SIZE = 3*8*4FIQ_STACK_SIZE = 0x004ABT_STACK_S ...