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 ...
随机推荐
- Cocos2d-x v3.1 核心类Director,Scene,Layer和Sprite(六)
Cocos2d-x v3.1 核心类Director,Scene,Layer和Sprite(六) Scene就像一个舞台一样在上面会摆放各种的元素,有的是固定的比如说布景,道具都是固定不动的,但有的元 ...
- 【Android开发笔记】生命周期研究
启动 onCreate onStart onResume 退出键 onPause onStop onDestroy 锁屏 & 按住 home键 & 被其他Activity覆盖(Sing ...
- uLua学习之读取外部Lua脚本(四)
前言 上节说到了Lua脚本与unity3d中C#脚本的数据交互,但是我感觉上节中的数理方式不太好,因为我们是把Lua脚本以字符串形式粘贴到C#脚本中的,如果读取配置数据都这样做的话,那就太可怕了.想想 ...
- 关于android界面菜单,project显示问题
刚用android studio不久,遇到了一个问题:界面菜单project不小心被关掉了,百度各种搜索无结果.我想大多数人都遇到过类似问题,此类问题其实很简单但是对于初学者来说就有点苦恼了.所以在此 ...
- pta 编程题10 Root of AVL Tree
其它pta数据结构编程题请参见:pta 这道题考察平衡二叉查找树的插入. 为了保证二叉查找树的平衡,当一个结点的左右子树的高度差大于1时就要进行调整. 分为以下四种情况: 插入新节点后,以及旋转之后, ...
- Using an Image for the Layer’s Content
Using an Image for the Layer’s Content Because a layer is just a container for managing a bitmap ima ...
- Portal简介
Portal 在英语中是入口的意思.Portal 认证通常也称为 Web 认证,一般将 Portal 认 证网站称为门户网站. 未认证用户上网时,设备强制用户登录到特定站点,用户可以免费访问其中的服务 ...
- mac 扫描存活IP段
法一:脚本内容 MacdeMacBook-Pro:~ root# cat ping-ttl.sh #!/bin/bash [ $UID -ne 0 ]&&{ echo "Pl ...
- web跨域及cookie相关知识总结
原文:web跨域及cookie相关知识总结 之前对于跨域相关的知识一致都很零碎,正好现在的代码中用到了跨域相关的,现在来对这些知识做一个汇总整理,方便自己查看,说不定也可能对你有所帮助. 本篇主要 ...
- SummerVocation_Learning--java的String类运用
题目: 编写一个程序,输出一个字符串中的大写字母数,小写字母数,及其它字母数. 思路1: 可以先遍历整个字符串,在判断每个字符的类型. public class TestString { public ...