leetcode 【 Reorder List 】python 实现
题目:
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}.
代码: oj 测试通过 248 ms
# 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 is None or head.next is None or head.next.next is None:
return head dummyhead = ListNode(0)
dummyhead.next = head # get the length of the linked list
p = head
list_length = 0
while p is not None:
list_length += 1
p = p.next #reverse the second half linked list
fast = dummyhead
for i in range((list_length+1)/2):
fast = fast.next
pre = fast
curr = pre.next
for i in range( (list_length)/2 - 1 ):
tmp = curr.next
curr.next = tmp.next
tmp.next = pre.next
pre.next = tmp #merge
h2 = pre.next
fast.next = None # cut the connection between 1st half linked list and 2nd half linked list
while head is not None and h2 is not None:
tmp = head.next
head.next = h2
tmp2 = h2.next
head.next.next = tmp
h2 = tmp2
head = tmp return dummyhead.next
思路:
这道题的路子分三块:
1. 遍历单链表 求链表长度
2. 锁定后半个链表,反转后半个链表的每个元素
3. 切断前后半个链表的链接处 然后合并两个链表
leetcode 【 Reorder List 】python 实现的更多相关文章
- [leetcode]Reorder List @ Python
原题地址:http://oj.leetcode.com/problems/reorder-list/ 题意: Given a singly linked list L: L0→L1→…→Ln-1→Ln ...
- 【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 ...
随机推荐
- Visual Studio 2010 vs2010 英文版 使用 已有的中文版 MSDN 帮助文档
第一步 设置Help Library Manager区域语言 打开Microsoft Visual Studio 2010开始菜单里Visual Studio Tools里的Manage Help S ...
- 8. String to Integer
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- SAP Netweaver的负载均衡消息服务器 vs CloudFoundry的App Router
Message server for ABAP Netweaver SAP传统应用经典的三层架构: 起到负载均衡的消息服务器(Message Server)在图中没有得到体现.然后,消息服务器在我们每 ...
- 如何在ABAP Netweaver和CloudFoundry里记录并查看日志
Netweaver 要记录日志需要有一个checkpoint group,可以自行创建也可以使用标准的.这里我重用标准的group:DEMO_CHECKPOINT_GROUP. tcode SAAB, ...
- 前端知识体系之CSS及其预处理器SASS/LESS
如果你是个前端设计师,很多时候我们都在写CSS,CSS是定义页面样式的脚本,并不是一种编程语言,只是一行行单纯的描述页面元素的样子,如果对英语熟练的话,它像说话一样简单,这里举个简单的例子: body ...
- NOIP2018赛前停课集训记——最后的刷板子计划
前言 再过两天就\(NOIP2018\)了. 于是,我决定不做其他题目,开始一心一意刷板子了. 这篇博客记录的就是我的刷板子计划. [洛谷3383][模板]线性筛素数 这种普及-的题目我还写挂了两次( ...
- iOS 常用正则表达式
今天看到一个正则表达式的文章,总结的挺好的,就自己转载一下,我还会陆续加入一些我自己看到常用的正则表达式 (原地址:http://www.code4app.com/blog-721976-112.ht ...
- vue组件 $children,$refs,$parent的使用
如果项目很大,组件很多,怎么样才能准确的.快速的寻找到我们想要的组件了?? 1)$refs 首先你的给子组件做标记.demo :<firstchild ref="one"&g ...
- Object.prototype.toString的应用
使用Object.prototype上的原生toString()方法判断数据类型,使用方法如下: Object.prototype.toString.call(value)1.判断基本类型: Obje ...
- C语言exp()函数:e的次幂函数(以e为底的x次方值)
头文件:#include <math.h> exp()用来计算以e 为底的x 次方值,即ex 值,然后将结果返回.其原型为: double exp(double x); [返回值]返 ...