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 ...
随机推荐
- 【Mood-14】龙虎榜 活跃在github中的1000位中国开发者
Last cache created on 2015-01-07 by Github API v3. ♥ made by hzlzh just for fun. Rank Gravatar usern ...
- 百倍性能的PL/SQL优化案例(r11笔记第13天)
我相信你是被百倍性能的字样吸引了,不过我所想侧重的是优化的思路,这个比优化技巧更重要,而结果嘛,其实我不希望说成是百倍提升,“”自黑“”一下. 有一个真实想法和大家讨论一下,就是一个SQL语句如果原本 ...
- @Valid的坑
@Valid 只能用来验证 @RequestBody 标注的参数,并且要写在 @RequestBody 之前
- nrm—源管理工具
全局安装 npm install -g nrm 查看可选源 nrm ls 其中,带*的是当前使用的源,上面的输出表明当前源是hiknpm 切换源 nrm use taobao 新增源 nrm add ...
- 比特币中P2SH(pay-to-script-hash)多重签名的锁定脚本和解锁脚本
P2SH(pay-to-script-hash)多重签名的脚本 P2SH是多重签名的一种应用形式.在P2SH的交易中,多了一个Redeem Script的概念,称为赎回脚本.当向P2SH脚本的地址转账 ...
- C++指针的概念解读
C++指针的概念解读 超详细 指针是一个特殊的变量,它里面存储的数值被解释成为内存里的一个地址.要搞清一个指针需要搞清指针的四方面的内容:指针的类型,指针所指向的类型,指针的值或者叫指针所指向的内存区 ...
- 【BZOJ3460】Jc的宿舍(树上莫队+树状数组)
点此看题面 大致题意: 一棵树,每个节点有一个人,他打水需要\(T_i\)的时间,每次询问两点之间所有人去打水的最小等待时间. 伪·强制在线 这题看似强制在线,但实际上,\(pre\ mod\ 2\) ...
- Windows8.1任务栏取消oneDrive图标
Windows8.1任务栏会有oneDrive图标,用不着,想取消,方法如下:
- 一次线上CPU高的问题排查实践
一次线上CPU高的问题排查实践 前言 近期某一天上班一开电脑,就收到了运维警报,有两台服务CPU负载很高,同时收到一线同事反馈 系统访问速度非常慢,几乎无响应. 一个美好的早晨,最怕什么就来什么.只好 ...
- React后台管理系统-登录页面
登录页面 <div className="col-md-4 col-md-offset-4"> <div className=&qu ...