leetcode 【 Partition List 】python 实现
题目:
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.
You should preserve the original relative order of the nodes in each of the two partitions.
For example,
Given 1->4->3->2->5->2 and x = 3,
return 1->2->2->4->3->5.
代码:oj测试通过 Runtime: 53 ms
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
# @param head, a ListNode
# @param x, an integer
# @return a ListNode
def partition(self, head, x):
if head is None or head.next is None:
return head h1 = ListNode(0)
dummyh1 = h1
h2 = ListNode(0)
dummyh2 = h2 p = head
while p is not None:
if p.val < x :
h1.next = p
h1 = h1.next
else:
h2.next = p
h2 = h2.next
p = p.next h1.next = dummyh2.next
h2.next = None
思路:
这道题的意思就是:把比x小的单拎出来,把大于等于x的单拎出来,再把两个Linked List首尾接起来。
技巧是设定两个虚拟表头dummyh1和dummyh2:保留住两个新表头。
另外需要设定两个迭代变量h1和h2:用于迭代变量
leetcode 【 Partition List 】python 实现的更多相关文章
- [leetcode]Partition List @ Python
原题地址:https://oj.leetcode.com/problems/partition-list/ 题意: Given a linked list and a value x, partiti ...
- [LeetCode]题解(python):086 - Partition List
题目来源 https://leetcode.com/problems/partition-list/ Given a linked list and a value x, partition it s ...
- [LeetCode]题解(python):131-Palindrome Partitioning
题目来源: https://leetcode.com/problems/palindrome-partitioning/ 题意分析: 给定一个字符串s,将s拆成若干个子字符串,使得所有的子字符串都是回 ...
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [LeetCode] Partition List 划分链表
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- [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 ...
随机推荐
- HTML和CSS中一些有趣的
CSS Rese http://yui.yahooapis.com/3.18.1/build/cssreset/cssreset-min.css <link rel="styleshe ...
- cms-登陆
先介绍下登陆的思路: 1.在登陆页面首先前端验证用户名和密码是否正确,如果验证通过,则ajax的方式向后台提交数据. 2.在controller层,将得到的用户名名和密码封装进shiro的token, ...
- UOJ#122【NOI2013】树的计数
[NOI2013]树的计数 链接:http://uoj.ac/problem/122 按BFS序来,如果$B_i$与$B_{i-1}$必须在同一层,那么贡献为0,必须在不同层那么贡献为1,都可以贡献为 ...
- IOS NSKeyedArchiver(归档存取数据)
如果对象是NSString.NSDictionary.NSArray.NSData.NSNumber等类 型,可以直接用NSKeyedArchiver进行归档和恢复 不是所有的对象都可以直接用这种方法 ...
- 【BZOJ4650】[NOI2016] 优秀的拆分(后缀数组)
点此看题面 大致题意: 定义将一个字符串拆成\(AABB\)的形式为优秀拆分,求一个字符串所有子串的优秀拆分个数. 后缀数组 这题可是一道后缀数组黑题啊. 其实看完题解这题还是挺简单的. 大致思路 显 ...
- python redis基本操作
#!/usr/bin/env python #-*- coding:utf-8 -*- # author:leo # datetime:2019/4/24 16:26 # software: PyCh ...
- Elastic Stack 安装
Elastic Stack 是一套支持数据采集.存储.分析.并可视化全面的分析工具,简称 ELK(Elasticsearch,Logstash,Kibana)的缩写. 安装Elastic Stack ...
- phpstorm —— Xdebug 的配置和使用
给phpstorm 配置Xdebug(Xdebug 是 PHP 的一个扩展, 用于帮助调试和开发.它包含一个与 ide 一起使用的单步调试器.它升级了 PHP 的 var_dump () 功能) 这篇 ...
- Java - BigDecimal四舍五入注意事项
如上图,精度后只有一位时,是五舍六入. 如上图,精度后只有第一位不为0时,也是五舍六入. 如上图,精度后至少有两位不为0时,才是四舍五入.
- Docker容器学习--1
Docker是PaaS 提供商 dotCloud 开源的一个基于 LXC 的高级容器引擎,源代码托管在 Github 上, 基于go语言并遵从Apache2.0协议开源.Docker是通过内核虚拟化技 ...